POJ 1743 Musical Theme (后缀数组+二分)
Musical Theme
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!
Input
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.
Output
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.
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 0Sample Output
5Hint
Use scanf instead of cin to reduce the read time.
分析: 题意要求最大不重复子串,可以用后缀数组。
题目中的变调会导致数字不固定,但它们之间的差是固定的,固用数组储存它们的差,进行后缀数组的操作
可以采用二分不断枚举结果值;
连续的后缀的最长公共前缀为它们之间最小的height值,由此确定二分的check函数
需要注意的是check函数中,maxx-minn>k 而不是>=
因为此时数组中保存的是差值,等于k的时候,会有一个数重叠。
比如
9
1 1 1 1 1 1 1 1 1
差值的最大不重复子串是4,但数的最大不重复子串却不等于4+1
代码如下:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> typedef long long ll; using namespace std; const int MAXN=200010; int wa[MAXN],wb[MAXN],wv[MAXN],Ws[MAXN]; char str[MAXN]; int a[MAXN]; int cmp(int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];} void da(int r[],int sa[],int n,int m) //n为len+1,m一般比数组中最大的数大一点即可 { int i,j,p,*x=wa,*y=wb,*t; for(i=0; i<m; i++) Ws[i]=0; for(i=0; i<n; i++) Ws[x[i]=r[i]]++; for(i=1; i<m; i++) Ws[i]+=Ws[i-1]; for(i=n-1; i>=0; i--) sa[--Ws[x[i]]]=i; for(j=1,p=1; p<n; j*=2,m=p) { for(p=0,i=n-j; i<n; i++) y[p++]=i; for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j; for(i=0; i<n; i++) wv[i]=x[y[i]]; for(i=0; i<m; i++) Ws[i]=0; for(i=0; i<n; i++) Ws[wv[i]]++; for(i=1; i<m; i++) Ws[i]+=Ws[i-1]; for(i=n-1; i>=0; i--) sa[--Ws[wv[i]]]=y[i]; for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; } return; } int sa[MAXN],Rank[MAXN],height[MAXN]; void calheight(int *r,int *sa,int n) { int i,j,k=0; for(i=1; i<=n; i++) Rank[sa[i]]=i; for(i=0; i<n; height[Rank[i++]]=k) for(k?k--:0,j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++); for(int i=n;i>=1;--i) ++sa[i],Rank[i]=Rank[i-1]; } int check(int k,int n) { int maxx=sa[1]; int minn=sa[1]; for(int i=2;i<=n;i++) { if(height[i]<k)maxx=minn=sa[i]; else { maxx=max(maxx,sa[i]); minn=min(minn,sa[i]); if(maxx-minn>k)return 1; } } return 0; } int main() { int n,maxx,ans; while(scanf("%d",&n)!=EOF) { maxx=0; ans=0; if(n==0) break; for(int i=0;i<n;i++){ scanf("%d",&a[i]); } for(int i=0;i<n-1;i++) a[i]=a[i+1]-a[i]+90; n=n-1;
a[n]=0; da(a,sa,n+1,200); calheight(a,sa,n); int l=0; int r=n/2+1; int mid,ans; while(l+1<r) { mid=(l+r)/2; if(check(mid,n)) l=mid; else r=mid; } ans=l; ans<4?printf("0\n"):printf("%d\n",ans+1); } return 0; }