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 last test case is followed by one zero.
Output
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
Hint
题解:最大不重叠相似子串。
参考代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; const int maxn=20020; int n,s[maxn]; int rk[maxn],sa[maxn],height[maxn]; int x[maxn<<1],y[maxn<<1],c[maxn]; inline void get_SA(int m) { for(int i=1;i<=m;++i) c[i]=0; for(int i=1;i<=n;++i) ++c[x[i]=s[i]]; for(int i=2;i<=m;++i) c[i]+=c[i-1]; for(int i=n;i>=1;--i) sa[c[x[i]]--]=i; for(int k=1;k<=n;k<<=1) { int num=0; for(int i=n-k+1;i<=n;++i) y[++num]=i; for(int i=1;i<=n;++i) if(sa[i]>k) y[++num]=sa[i]-k; for(int i=1;i<=m;++i) c[i]=0; for(int i=1;i<=n;++i) ++c[x[i]]; for(int i=2;i<=m;++i) c[i]+=c[i-1]; for(int i=n;i>=1;--i) sa[c[x[y[i]]]--]=y[i],y[i]=0; swap(x,y); x[sa[1]]=1; num=1; for(int i=2;i<=n;++i) x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k])?num:++num; if(num==n) break; m=num; } } inline void get_height() { int k=0; for(int i=1;i<=n;++i) rk[sa[i]]=i; for(int i=1;i<=n;++i) { if(rk[i]==1) continue; if(k) --k; int j=sa[rk[i]-1]; while(j+k<=n&&i+k<=n&&s[i+k]==s[j+k]) ++k; height[rk[i]]=k; } } bool check(int k) { int mx=-INF,mi=INF; for(int i=1;i<=n;++i) { if(height[i]>=k) { mx=max(mx,max(sa[i],sa[i-1])); mi=min(mi,min(sa[i],sa[i-1])); if(mx-mi>k) return true; } else mx=-INF,mi=INF; } return false; } int main() { while(scanf("%d",&n) && n) { int pre,now; n--; scanf("%d",&pre); for(int i=1;i<=n;++i) scanf("%d",&now),s[i]=now-pre+88,pre=now; get_SA(176); get_height(); int l=1,r=n>>1; while(l+1<r) { int mid=l+r>>1; if(check(mid)) l=mid; else r=mid; } int ans; if(check(r)) ans=r; else ans=l; printf("%d ",ans>=4? ans+1:0); } return 0; }
后缀自动机
/********* 后缀自动机做法 ***********/ #include<map> #include<ctime> #include<queue> #include<cmath> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 1000000000 #define mod 1000000007 #define pa pair<int,int> #define ll long long using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,a[20005]; struct sam{ int last,cnt,ans; int l[40005],r[40005],mx[40005],fa[40005],a[40005][180]; int q[40005],v[40005]; void init() { memset(l,127,sizeof(l)); memset(r,0,sizeof(r)); memset(v,0,sizeof(v)); memset(mx,0,sizeof(mx)); memset(fa,0,sizeof(fa)); memset(a,0,sizeof(a)); last=cnt=1;ans=0; } void extend(int c) { int p=last,np=last=++cnt;mx[np]=mx[p]+1; l[np]=r[np]=mx[np]; while(!a[p][c]&&p)a[p][c]=np,p=fa[p]; if(!p)fa[np]=1; else { int q=a[p][c]; if(mx[p]+1==mx[q]) fa[np]=q; else { int nq=++cnt;mx[nq]=mx[p]+1; memcpy(a[nq],a[q],sizeof(a[q])); fa[nq]=fa[q]; fa[np]=fa[q]=nq; while(a[p][c]==q) a[p][c]=nq,p=fa[p]; } } } void solve() { for(int i=1;i<=cnt;i++) v[mx[i]]++; for(int i=1;i<=n;i++) v[i]+=v[i-1]; for(int i=cnt;i;i--) q[v[mx[i]]--]=i; for(int i=cnt;i;i--) { int p=q[i]; l[fa[p]]=min(l[fa[p]],l[p]); r[fa[p]]=max(r[fa[p]],r[p]); } for(int i=1;i<=cnt;i++) ans=max(ans,min(mx[i],r[i]-l[i])); if(ans<4)puts("0"); else printf("%d ",ans+1); } } sam; int main() { while(scanf("%d",&n)) { if(n==0)break; for(int i=1;i<=n;i++) a[i]=read();n--; for(int i=1;i<=n;i++) a[i]=a[i+1]-a[i]+88; sam.init(); for(int i=1;i<=n;i++) sam.extend(a[i]); sam.solve(); } return 0; }