「POJ1743」Musical Theme
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:
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.
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
1 2 3 4 |
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
1 |
5 |
Hint
Use scanf instead of cin to reduce the read time.
题解
不可重叠最长重复子串
算法1:后缀数组
得出height数组后,二分答案x,将连续的>=x的段分组
如果一组内sa的最大值与最小值的差>=x,则x成立
算法2:后缀自动机
如果我们得出每个结点的right集合
就能得到这些串的出现位置
right集合就是在fail树中子树内结束结点的并集
可以dp出right集合中的最大值/最小值,差值和max取较小值就是到该结点串符合题意的最长长度
代码非常简单 <-hzwer这个傻逼模板打错了还在叽歪
后缀数组
2014.5.8 *1
2014.9.5 *1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<set> #include<ctime> #include<vector> #include<cmath> #include<algorithm> #include<map> #define N 20005 #define inf 2000000000 using namespace std; inline 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,p,q,k; int a[N],v[N],h[N]; int sa[2][N],rk[2][N]; void calsa(int sa[N],int rk[N],int SA[N],int RK[N]) { for(int i=1;i<=n;i++)v[rk[sa[i]]]=i; for(int i=n;i;i--) if(sa[i]>k) SA[v[rk[sa[i]-k]]--]=sa[i]-k; for(int i=n-k+1;i<=n;i++)SA[v[rk[i]]--]=i; for(int i=1;i<=n;i++) RK[SA[i]]=RK[SA[i-1]]+(rk[SA[i-1]]!=rk[SA[i]]||rk[SA[i]+k]!=rk[SA[i-1]+k]); } void getsa() { memset(v,0,sizeof(v)); p=0,q=1; for(int i=1;i<=n;i++)v[a[i]]++; for(int i=1;i<=200;i++)v[i]+=v[i-1]; for(int i=1;i<=n;i++) sa[p][v[a[i]]--]=i; for(int i=1;i<=n;i++) rk[p][sa[p][i]]=rk[p][sa[p][i-1]]+(a[sa[p][i-1]]!=a[sa[p][i]]); for(k=1;k<n;k<<=1,swap(p,q)) calsa(sa[p],rk[p],sa[q],rk[q]); } void geth() { k=0; for(int i=1;i<=n;i++) if(rk[p][i]==1)h[rk[p][i]]=0; else { int j=sa[p][rk[p][i]-1]; while(a[i+k]==a[j+k])k++; h[rk[p][i]]=k;if(k>0)k--; } } bool jud(int x) { int mn=sa[p][1],mx=sa[p][1]; for(int i=2;i<=n;i++) if(h[i]<x) mn=mx=sa[p][i]; else { mn=min(mn,sa[p][i]); mx=max(mx,sa[p][i]); if(mx-mn>=x)return 1; } return 0; } void solve() { int l=0,r=n/2,ans; while(l<=r) { int mid=(l+r)>>1; if(jud(mid))ans=mid,l=mid+1; else r=mid-1; } if(ans<4)puts("0"); else printf("%d\n",ans+1); } 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]+100; getsa(); geth(); solve(); } return 0; } |
后缀自动机 2015.4.10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include<set> #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\n",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; } |
黄学长,你的后缀数组的代码,判断忘记+1了,然后Wa了