Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 18091 | Accepted: 6203 |
Description
Input
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<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<cstring> using namespace std; int x,n,i,xzq,mm; int a[20011],rank[20011],sa[20011],h[20011],b[20011]; int co[20011],nr[20011],s[20011]; void sort(int *a) { int i,mn; if(mm>n)mn=mm; else mn=n; for(i=0;i<=mn;i++)co[i]=0; for(i=1;i<=n;i++)co[a[i]+1]++; for(i=1;i<=mn;i++)co[i]+=co[i-1]; for(i=1;i<=n;i++)nr[i]=0; for(i=1;i<=n;i++){ co[a[sa[i]]]++; nr[co[a[sa[i]]]]=sa[i]; } for(i=1;i<=n;i++)sa[i]=nr[i]; } void getrank() { int i; x=0; for(i=1;i<=n;i++){ if(i==1||a[sa[i]]!=a[sa[i-1]]||b[sa[i]]!=b[sa[i-1]])x++; rank[sa[i]]=x; } } void Suffix() { int i,l,last,j,k; for(i=1;i<=n;i++){ sa[i]=i; b[i]=0; } sort(a); getrank(); l=1; while(x!=n){ for(i=1;i<=n;i++){ a[i]=rank[i]; if(i+l<=n)b[i]=rank[i+l]; else b[i]=0; } sort(b); sort(a); getrank(); l*=2; } last=0; for(i=1;i<=n;i++){ if(last)last--; if(rank[i]==1)continue; j=i; k=sa[rank[i]-1]; while(j+last<=n&&k+last<=n&&s[j+last]==s[k+last])last++; h[rank[i]]=last; } } bool check(int x) { int i,minn,maxn; minn=2147483647; maxn=0; for(i=2;i<=n+1;i++){ if(sa[i-1]<minn)minn=sa[i-1]; if(sa[i-1]>maxn)maxn=sa[i-1]; if(h[i]<x){ if(maxn-minn>x)return true;//(必须是严格大于,这就是之前说的那个特判) maxn=0; minn=2147483647; } } return false; } void Work() { int l,r,mid; l=1; r=n; while(l<=r){ mid=(l+r)/2; if(check(mid))l=mid+1; else r=mid-1; } xzq=r; } int main() { while(true){ scanf("%d",&n); if(n==0)break; memset(sa,0,sizeof(sa)); memset(h,0,sizeof(h)); memset(rank,0,sizeof(rank)); for(i=1;i<=n;i++)scanf("%d",&a[i]); mm=0; for(i=1;i<n;i++){ a[i]=a[i+1]-a[i]+100; if(a[i]>mm)mm=a[i]; s[i]=a[i]; } mm++; n--; a[n]=0; Suffix(); Work(); xzq++; if(xzq<5)printf("0\n"); else printf("%d\n",xzq); } }
原文:http://www.cnblogs.com/applejxt/p/3863091.html