1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define LS 2*i 5 #define RS 2*i+1 6 #define UP(i,x,y) for(i=x;i<=y;i++) 7 #define DOWN(i,x,y) for(i=x;i>=y;i--) 8 #define MEM(a,x) memset(a,x,sizeof(a)) 9 #define W(a) while(a) 10 #define gcd(a,b) __gcd(a,b) 11 #define LL long long 12 #define N 20005 13 #define MOD 1000000007 14 #define INF 0x3f3f3f3f 15 #define EXP 1e-8 16 int wa[N],wb[N],wsf[N],wv[N],sa[N]; 17 int rank1[N],height[N],s[N],a[N],n; 18 char str1[N],str2[N]; 19 //sa:字典序中排第i位的起始位置在str中第sa[i] 20 //rank:就是str第i个位置的后缀是在字典序排第几 21 //height:字典序排i和i-1的后缀的最长公共前缀 22 int cmp(int *r,int a,int b,int k) 23 { 24 return r[a]==r[b]&&r[a+k]==r[b+k]; 25 } 26 void getsa(int *r,int *sa,int n,int m)//n要包含末尾添加的0 27 { 28 int i,j,p,*x=wa,*y=wb,*t; 29 for(i=0; i<m; i++) wsf[i]=0; 30 for(i=0; i<n; i++) wsf[x[i]=r[i]]++; 31 for(i=1; i<m; i++) wsf[i]+=wsf[i-1]; 32 for(i=n-1; i>=0; i--) sa[--wsf[x[i]]]=i; 33 p=1; 34 j=1; 35 for(; p<n; j*=2,m=p) 36 { 37 for(p=0,i=n-j; i<n; i++) y[p++]=i; 38 for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j; 39 for(i=0; i<n; i++) wv[i]=x[y[i]]; 40 for(i=0; i<m; i++) wsf[i]=0; 41 for(i=0; i<n; i++) wsf[wv[i]]++; 42 for(i=1; i<m; i++) wsf[i]+=wsf[i-1]; 43 for(i=n-1; i>=0; i--) sa[--wsf[wv[i]]]=y[i]; 44 t=x; 45 x=y; 46 y=t; 47 x[sa[0]]=0; 48 for(p=1,i=1; i<n; i++) 49 x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++; 50 } 51 } 52 void getheight(int *r,int n)//n不保存最后的0 53 { 54 int i,j,k=0; 55 for(i=1; i<=n; i++) rank1[sa[i]]=i; 56 for(i=0; i<n; i++) 57 { 58 if(k) 59 k--; 60 else 61 k=0; 62 j=sa[rank1[i]-1]; 63 while(r[i+k]==r[j+k]) 64 k++; 65 height[rank1[i]]=k; 66 } 67 } 68 int ans; 69 int fun(int k) 70 { 71 int i,maxn,minn; 72 maxn = minn = sa[1]; 73 UP(i,2,n) 74 { 75 if(height[i]>=k && i<n) 76 { 77 minn = min(minn,sa[i]); 78 maxn = max(maxn,sa[i]); 79 continue; 80 } 81 if(maxn-minn>=k) return 1; 82 maxn = minn = sa[i]; 83 } 84 return 0; 85 } 86 87 88 int main() 89 { 90 int i,j,k; 91 W((~scanf("%d",&n),n)) 92 { 93 UP(i,0,n-1) 94 { 95 scanf("%d",&s[i]); 96 } 97 UP(i,0,n-2) 98 { 99 s[i] = s[i+1]-s[i]+100; 100 } 101 s[--n] = 0; 102 getsa(s,sa,n+1,200); 103 getheight(s,n); 104 int l = 4,r = n; 105 W(l<=r) 106 { 107 int mid = (l+r)/2; 108 if(fun(mid)) 109 { 110 ans = mid; 111 l=mid+1; 112 } 113 else r = mid-1; 114 } 115 ans++; 116 printf("%d ",ans<5?0:ans); 117 } 118 119 return 0; 120 }