http://poj.org/problem?id=1743
今天下午写这个调了2个小时。。。。。。
现在基本上,抄着模板没什么问题了
基本应用——不可重叠最长重复子串
//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const int read()
{int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/
const int m=177;
int n;
int s[20002];
int sa[20002],height[20002],rk[20002];
int t1[20002],t2[20002],c[20002];
/
void get_sa(int *x,int *y,int *sa,int n,int m)
{
rep(i,0,m-1) c[i]=0;
rep(i,0,n-1) c[x[y[i]]]++;
rep(i,1,m-1) c[i]+=c[i-1];
per(i,n-1,0) sa[--c[x[y[i]]]]=y[i];
}
void get_height(int *s,int *sa,int *height,int n)
{
int i,j,k=0;
rep(i,1,n) rk[sa[i]]=i;
for(i=0;i<n;height[rk[i++]]=k)
for(k?k--:0,j=sa[rk[i]-1];s[i+k]==s[j+k];k++);
}
void da(int *s,int *sa,int n)
{
int i,j,p,m=::m,*x=t1,*y=t2,*t;
rep(i,0,n-1) x[i]=s[i],y[i]=i;
get_sa(x,y,sa,n,m);
for(j=p=1;p<n;j<<=1,m=p)
{
p=0;
rep(i,n-j,n-1) y[p++]=i;
rep(i,0,n-1) if(sa[i]>=j) y[p++]=sa[i]-j;
get_sa(x,y,sa,n,m);
t=x;x=y;y=t;p=1;x[sa[0]]=0;
rep(i,1,n-1) x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;
}
get_height(s,sa,height,n);
}
bool check(int md)
{
int ma,mi;
ma=mi=sa[1];
rep(i,2,n)
{
if(height[i]<md) ma=mi=sa[i];
else
{
ma=max(ma,sa[i]);
mi=min(mi,sa[i]);
if(ma-mi>md) return 1;
}
}
return 0;
}
/
void input()
{
MS(s,0); MS(sa,0); MS(height,0); MS(rk,0);
MS(t1,0); MS(t2,0); MS(c,0);
rep(i,1,n) s[i]=read();
n--;
rep(i,1,n) s[i]=s[i+1]-s[i]+88;
}
void solve()
{
da(s,sa,n);
int l=1,r=n,mid;
while(l<r)
{
mid=l+r+1>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
l++;
if(l<5) puts("0");
else printf("%d\n",l);
}
/
int main()
{
#ifndef _TEST
freopen("std.in","r",stdin); freopen("std.out","w",stdout);
#endif
while(scanf("%d",&n),n)
input(),solve();
return 0;
}