标签:
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
Source
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 #ifdef WIN32 14 #define OT "%I64d" 15 #else 16 #define OT "%lld" 17 #endif 18 using namespace std; 19 typedef long long LL; 20 const int MAXN = 20011; 21 int ch[MAXN]; 22 int n,m; 23 int wa[MAXN],wb[MAXN],c[MAXN]; 24 int rank[MAXN],height[MAXN]; 25 int sa[MAXN]; 26 int ans; 27 28 inline int getint() 29 { 30 int w=0,q=0; 31 char c=getchar(); 32 while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); 33 if (c==‘-‘) q=1, c=getchar(); 34 while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); 35 return q ? -w : w; 36 } 37 38 inline void da(int m,int n){ 39 int i,*x=wa,*y=wb; 40 for(i=1;i<=m;i++) c[i]=0; 41 for(i=1;i<=n;i++) c[x[i]=ch[i]]++; 42 for(i=1;i<=m;i++) c[i]+=c[i-1]; 43 for(i=n;i>=1;i--) sa[c[x[i]]--]=i; 44 for(int k=1,p;k<=n;k=k*2) { 45 p=0; 46 for(i=n-k+1;i<=n;i++) y[++p]=i; 47 for(i=1;i<=n;i++) if(sa[i]>k) y[++p]=sa[i]-k; 48 for(i=1;i<=m;i++) c[i]=0; 49 for(i=1;i<=n;i++) c[x[y[i]]]++; 50 for(i=1;i<=m;i++) c[i]+=c[i-1]; 51 for(i=n;i>=1;i--) sa[c[x[y[i]]]--]=y[i]; 52 swap(x,y); x[sa[1]]=1; p=1; 53 for(i=2;i<=n;i++) x[sa[i]]=(y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k])?p:++p; 54 if(p==n) break; m=p; 55 } 56 } 57 58 inline void calheight(){ 59 int i,j,k=0; 60 for(i=1;i<=n;i++) { 61 if(k) k--; 62 j=sa[rank[i]-1]; 63 while(ch[i+k]==ch[j+k]) k++; 64 height[rank[i]]=k;//是rank[i]!!! 65 } 66 } 67 68 inline bool check(int x){ 69 int i=2,minl,maxl; 70 while(1) { 71 while(i<=n && height[i]<x) i++; 72 if(i>n) break; 73 minl=maxl=sa[i-1]; 74 while(i<=n && height[i]>=x) minl=min(minl,sa[i]),maxl=max(maxl,sa[i]),i++; 75 if(maxl-minl>x) return true;//不能等号!!!记录的是差 76 } 77 return false; 78 } 79 80 inline void work(){ 81 while(1) { 82 n=getint(); if(n==0) break; 83 memset(ch,0,sizeof(ch)); memset(sa,0,sizeof(sa)); 84 memset(wa,0,sizeof(wa)); memset(wb,0,sizeof(wb)); 85 memset(height,0,sizeof(height)); memset(rank,0,sizeof(rank)); 86 for(int i=1;i<=n;i++) ch[i]=getint(); 87 if(n<10) { printf("0\n"); continue; } 88 for(int i=1;i<n;i++) ch[i]=ch[i+1]-ch[i],ch[i]+=89; 89 ch[n]=0; 90 da(300,n); 91 for(int i=1;i<=n;i++) rank[sa[i]]=i; 92 calheight(); 93 int l=1,r=n,mid; 94 while(l<r) { 95 mid=(l+r)/2; 96 if(check(mid)) l=mid+1; 97 else r=mid; 98 } 99 if(l<=4) printf("0\n"); 100 else printf("%d\n",l); 101 } 102 } 103 104 int main() 105 { 106 work(); 107 return 0; 108 }
标签:
原文地址:http://www.cnblogs.com/ljh2000-jump/p/5659920.html