题目链接:Musical Theme
题意:给一个数组表示一段音乐,范围1~88;问为最长的的位置不相交的相似的旋律。相似的意思是,整段加上某个值,比如1 2 3 4 5 和 6 7 8 9 10是相似。
题解:求出后缀数组,高度数组lcp,枚举答案x ,找到一个对于连续的lcp[i]>=x,找到看最小的sa[i],最大的sa[i],看是否差值大于l,就可以判断是否重叠。
//#include<bits/stdc++.h> #include<iostream> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #define pb push_back #define ll long long #define PI 3.14159265 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define ws wppp #define eps 1e-7 using namespace std; const int N=2e4+5; const int mod=1e9+7; int a[N], s[N]; int sa[N], t[N], t2[N], c[N], n; int ran[N], height[N]; const int inf=0x3fffffff; void get_sa(int m) { int i, *x = t, *y = t2; for(i = 0; i < m; i++) c[i] = 0; for(i = 0; i < n; i++) c[x[i] = s[i]]++; for(i = 1; i < m; i++) c[i] += c[i-1]; for(i = n-1; i >= 0; i--) sa[--c[x[i]]] = i; for(int k = 1; k <= n; k <<= 1) { int p = 0; for(i = n-k; i < n; i++) y[p++] = i; for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k; for(i = 0; i < m; i++) c[i] = 0; for(i = 0; i < n; i++) c[x[y[i]]]++; for(i = 0; i< m; i++) c[i] += c[i-1]; 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]+k] == y[sa[i]+k] ? p-1 : p++; if(p >= n) break; m = p; } int k = 0; for(i = 0; i < n; i++) ran[sa[i]] = i; for(i = 0; i < n; i++) { if(k) k--; int j = sa[ran[i]-1]; while(s[i+k] == s[j+k]) k++; height[ran[i]] = k; } } bool judge(int x) { int mi=inf,mx=-inf; for(int i=1;i<n;i++) { if(height[i]>=x) { mi=min(mi,min(sa[i],sa[i-1])); mx=max(mx,max(sa[i],sa[i-1])); if(mx-mi>x)return true; } else { mi=inf,mx=-inf; } } return false; } int main() { while(scanf("%d",&n)&&n) { for(int i=0;i<n;i++)scanf("%d",&a[i]); for(int i=0;i<n-1;i++) { s[i]=a[i+1]-a[i]+100; } s[n-1]=0; get_sa(200); int l=0,r=n; while(l<=r) { int mid=l+r>>1; if(judge(mid))l=mid+1; else r=mid-1; } if(r>=4)printf("%d ",r+1); else printf("0 "); } return 0; }