Musical Theme
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 28409 | Accepted: 9591 |
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)
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
3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 78 74 70 66 67 64 60 65 800
Sample Output
5
Hint
Use scanf instead of cin to reduce the read time.
Source
题意:
求两个最长相似子串(差分序列相同)的长度
/*得出height数组后,二分答案x,将连续的>=x的段分组如果一组内sa的最大值与最小值的差>=x,则x成立*/#include#include #include using namespace std;const int N=2e4+5;int n,sa[N],tsa[N],rank[N],trank[N],c[N],h[N];int s[N];void DA(int maxx=256){ memset(c,0,sizeof c);int p; for(int i=1;i<=n;i++) c[rank[i]=s[i]]++; for(int i=2;i<=maxx;i++) c[i]+=c[i-1]; for(int i=n;i;i--) sa[c[rank[i]]--]=i; trank[sa[1]]=p=1; for(int i=2;i<=n;i++){ if(rank[sa[i]]!=rank[sa[i-1]]) p++; trank[sa[i]]=p; } for(int i=1;i<=n;i++) rank[i]=trank[i]; for(int k=1;p <<=1,maxx=p){ p=0; for(int i=n-k+1;i<=n;i++) tsa[++p]=i; for(int i=1;i<=n;i++) if(sa[i]>k) tsa[++p]=sa[i]-k; memset(c,0,sizeof c); for(int i=1;i<=n;i++) trank[i]=rank[tsa[i]]; for(int i=1;i<=n;i++) c[trank[i]]++; for(int i=2;i<=maxx;i++) c[i]+=c[i-1]; for(int i=n;i;i--) sa[c[trank[i]]--]=tsa[i]; trank[sa[1]]=p=1; for(int i=2;i<=n;i++){ if(rank[sa[i]]!=rank[sa[i-1]]||rank[sa[i]+k]!=rank[sa[i-1]+k]) p++; trank[sa[i]]=p; } for(int i=1;i<=n;i++) rank[i]=trank[i]; } for(int i=1,k=0;i<=n;i++){ int j=sa[rank[i]-1]; while(s[i+k]==s[j+k]) k++; h[rank[i]]=k;if(k>0) k--; }}bool judge(int k){ int mn=sa[1],mx=sa[1]; for(int i=2;i<=n;i++){ if(h[i] =k) return 1; } } return 0;}void Clear(){ memset(h,0,sizeof h); memset(sa,0,sizeof sa); memset(tsa,0,sizeof tsa); memset(rank,0,sizeof rank); memset(trank,0,sizeof trank);}int main(){ while(scanf("%d",&n)==1){ if(!n) break; Clear(); for(int i=1;i<=n;i++) scanf("%d",&s[i]);n--; for(int i=1;i<=n;i++) s[i]=s[i+1]-s[i]+100; DA(); int l=0,r=n/2,mid,ans=0; while(l<=r){ mid=l+r>>1; if(judge(mid)) l=mid+1,ans=mid; else r=mid-1; } ans++; if(ans<5) ans=0; printf("%d\n",ans); } return 0;}