标签:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 24835 | Accepted: 8377 |
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
SA
#include<cstdio> #include<cstring> #include<algorithm> #define MN 20003 using namespace std; int n; char s1[MN]; int s[MN],a[MN]; int v[MN],sa[MN],q[MN],rank[MN],h[MN],mmh=0,len; inline void gr(int x){ rank[sa[1]]=1; for (int i=2;i<=n;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-1]]&&s[sa[i]+x]==s[sa[i-1]+x])?rank[sa[i-1]]:rank[sa[i-1]]+1; for (int i=1;i<=n;i++) s[i]=rank[i]; } inline void gv(){memset(v,0,sizeof(v));for (int i=1;i<=n;i++) v[s[i]]++;for (int i=1;i<=2e4;i++)v[i]+=v[i-1];} inline void gsa(){ gv();for (int i=n;i>=1;i--) sa[v[s[i]]--]=i;gr(0); for (int i=1;i<n;i<<=1){ gv();for (int j=n;j>=1;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i; for (int j=n-i+1;j<=n;j++) q[v[s[j]]--]=j; for (int j=1;j<=n;j++) sa[j]=q[j];gr(i); if (rank[sa[n]]==n) return; } } inline void gh(){for (int i=1,k=0,j;i<=n;h[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);} int main(){ scanf("%d",&n); while(n){ for (int i=1;i<=n;i++) scanf("%d",&a[i]); if(n<10){printf("0\n");scanf("%d",&n);continue;} n--; for (int i=1;i<=n;i++) s[i]=a[i+1]-a[i]+90;s[n+1]=0; for (int i=1;i<=n;i++) a[i]=s[i]; gsa();gh(); int l=4,r=2e4,mid,bo=1,ma,mi,i,j,k; while(l<r){ mid=(l+r+1)>>1; for (i=1,j,k=2;i<=n;i=k++){ ma=0;mi=2e4; while (h[k]>=mid&&k<=n) k++; for (j=i;j<k;j++){ if (ma<sa[j]) ma=sa[j]; if (mi>sa[j]) mi=sa[j]; } if (ma-mi>=mid) break; } if (i>n) r=mid-1;else l=mid; } l=l<4?0:l+1; printf("%d\n",l); scanf("%d",&n); } }
标签:
原文地址:http://www.cnblogs.com/Enceladus/p/5461544.html