思路:
后缀数组论文题
对后缀分组
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 20005
int n,s[N],cntA[N],cntB[N],A[N],B[N],rk[N],sa[N],tsa[N],ht[N];
void SA(){
memset(cntA,0,sizeof(cntA));
for(int i=1;i<=n;i++)cntA[s[i]]++;
for(int i=1;i<=200;i++)cntA[i]+=cntA[i-1];
for(int i=n;i;i--)sa[cntA[s[i]]--]=i;
rk[sa[1]]=1;
for(int i=2;i<=n;i++)rk[sa[i]]=rk[sa[i-1]]+(s[sa[i]]!=s[sa[i-1]]);
for(int l=1;rk[sa[n]]<n;l<<=1){
memset(cntA,0,sizeof(cntA));
memset(cntB,0,sizeof(cntB));
for(int i=1;i<=n;i++)
cntA[A[i]=rk[i]]++,
cntB[B[i]=i+l<=n?rk[i+l]:0]++;
for(int i=1;i<=n;i++)cntA[i]+=cntA[i-1],cntB[i]+=cntB[i-1];
for(int i=n;i;i--)tsa[cntB[B[i]]--]=i;
for(int i=n;i;i--)sa[cntA[A[tsa[i]]]--]=tsa[i];
rk[sa[1]]=1;
for(int i=2;i<=n;i++)rk[sa[i]]=rk[sa[i-1]]+(A[sa[i]]!=A[sa[i-1]]||B[sa[i]]!=B[sa[i-1]]);
}
for(int i=1,j=0;i<=n;i++){
j=j?j-1:0;
while(s[i+j]==s[sa[rk[i]-1]+j])j++;
ht[rk[i]]=j;
}
}
bool check(int x){
int minn=n+1,maxx=0;
for(int i=1;i<=n;i++){
if(ht[i]>=x){
minn=min(minn,sa[i]),maxx=max(maxx,sa[i]);
if(maxx-minn>x)return 1;
}
else minn=maxx=sa[i];
}return 0;
}
int main(){
while(scanf("%d",&n)&&n){
for(int i=1;i<=n;i++)scanf("%d",&s[i]);
for(int i=n;i;i--)s[i]=s[i]-s[i-1]+100;
SA();
int l=1,r=n,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid))l=mid+1,ans=mid;
else r=mid-1;
}
printf("%d\n",ans>=4?ans+1:0);
}
}