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:
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!
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!
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.
The last test case is followed by one zero.
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.
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
5
Use scanf instead of cin to reduce the read time.
#include<cstdio>
#include<cstring>
#include<algorithm>
#define Rep(i,n) for(i=0;i<(n);i++)//宏定义,减少下面的代码量
typedef long long ll;
using namespace std;
const int maxn=20010;
int str[maxn],num[maxn];
int s[maxn];
int sa[maxn],t1[maxn],t2[maxn],c[maxn];//c当做是桶
int rank[maxn],height[maxn];
void get_sa(int s[],int n,int m)
{
int i,j,p,*x=t1,*y=t2;
Rep(i,m) c[i]=0;
Rep(i,n) c[x[i]=s[i]]++;//同时x[i]=s[i]
Rep(i,m) c[i+1]+=c[i];
for(i=n-1; i>=0; i--) sa[--c[x[i]]]=i; //第一关键字基数排序
for(j=1; j<=n; j<<=1) {
p=0;
for(i=n-j; i<n; i++) y[p++]=i;
Rep(i,n) if(sa[i]>=j) y[p++]=sa[i]-j;
Rep(i,m) c[i]=0;
Rep(i,n) c[x[y[i]]]++;//同时x[i]=s[i]
Rep(i,m) c[i+1]+=c[i];
for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i]; //第一关键字基数排序
swap(x,y);
p=1;
x[sa[0]]=0;
for(i=1; i<n; i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
if(p>=n) break;
m=p;
}
}
void get_height(int s[],int n)
{
int i,j,k=0;
for(i=0; i<=n; i++) rank[sa[i]]=i;
Rep(i,n) {
if(k) k--;
j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
int check(int val,int n){
int mi=sa[1],mx=sa[1];
for(int i=2;i<n;i++){
if(height[i]<val){
mi=sa[i],mx=sa[i];
}
else{
mi=min(mi,sa[i]);
mx=max(mx,sa[i]);
if(mx-mi>=val)
return 1;
}
}
return 0;
}
int solve(int n){
int l=0,r=n/2,mid;
int ans=0;
while(l<=r){
mid=(l+r)/2;
if(check(mid,n)){
ans=max(ans,mid);
l=mid+1;
}
else
r=mid-1;
}
return ans>=4?ans+1:0;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n) {
int i;
Rep(i,n)
scanf("%d",&str[i]);
if(n<10){
printf("0\n");continue;
}
Rep(i,n-1) num[i]=str[i+1]-str[i]+90;
get_sa(num,n,180);
get_height(num,n-1);
int ans=solve(n-1);
printf("%d\n",ans);
}
return 0;
}