poj 1743

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 24835   Accepted: 8377

Description

A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical timing; but, this programming task is about notes and not timings. 
Many composers structure their music around a repeating &qout;theme&qout;, which, being a subsequence of an entire melody, is a sequence of integers in our representation. A subsequence of a melody is a theme if it:
  • is at least five notes long
  • appears (potentially transposed -- see below) again somewhere else in the piece of music
  • is disjoint from (i.e., non-overlapping with) at least one of its other appearance(s)

Transposed means that a constant positive or negative value is added to every note value in the theme subsequence. 
Given a melody, compute the length (number of notes) of the longest theme. 
One second time limit for this problem's solutions! 

Input

The input contains several test cases. The first line of each test case contains the integer N. The following n integers represent the sequence of notes. 
The last test case is followed by one zero. 

Output

For each test case, the output file should contain a single line with a single integer that represents the length of the longest theme. If there are no themes, output 0.

Sample Input

30
25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18
82 78 74 70 66 67 64 60 65 80
0

Sample Output

5

SA

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 20003
using namespace std; int n;
char s1[MN];
int s[MN],a[MN];
int v[MN],sa[MN],q[MN],rank[MN],h[MN],mmh=,len;
inline void gr(int x){
rank[sa[]]=;
for (int i=;i<=n;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-]]&&s[sa[i]+x]==s[sa[i-]+x])?rank[sa[i-]]:rank[sa[i-]]+;
for (int i=;i<=n;i++) s[i]=rank[i];
}
inline void gv(){memset(v,,sizeof(v));for (int i=;i<=n;i++) v[s[i]]++;for (int i=;i<=2e4;i++)v[i]+=v[i-];}
inline void gsa(){
gv();for (int i=n;i>=;i--) sa[v[s[i]]--]=i;gr();
for (int i=;i<n;i<<=){
gv();for (int j=n;j>=;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i;
for (int j=n-i+;j<=n;j++) q[v[s[j]]--]=j;
for (int j=;j<=n;j++) sa[j]=q[j];gr(i);
if (rank[sa[n]]==n) return;
}
}
inline void gh(){for (int i=,k=,j;i<=n;h[rank[i++]]=k) for (k?k--:,j=sa[rank[i]-];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);}
int main(){
scanf("%d",&n);
while(n){
for (int i=;i<=n;i++) scanf("%d",&a[i]);
if(n<){printf("0\n");scanf("%d",&n);continue;}
n--;
for (int i=;i<=n;i++) s[i]=a[i+]-a[i]+;s[n+]=;
for (int i=;i<=n;i++) a[i]=s[i];
gsa();gh();
int l=,r=2e4,mid,bo=,ma,mi,i,j,k;
while(l<r){
mid=(l+r+)>>;
for (i=,j,k=;i<=n;i=k++){
ma=;mi=2e4;
while (h[k]>=mid&&k<=n) k++;
for (j=i;j<k;j++){
if (ma<sa[j]) ma=sa[j];
if (mi>sa[j]) mi=sa[j];
}
if (ma-mi>=mid) break;
}
if (i>n) r=mid-;else l=mid;
}
l=l<?:l+;
printf("%d\n",l);
scanf("%d",&n);
}
}

940K 250MS G++ 1716B

 
上一篇:毕业设计(4):基于MicroPython的超声波倒车雷达系统


下一篇:TIDB技术文档翻译