顺着原字符串找到所有叶子节点,然后自下而上更新,每个节点right的最左和最右,然后求出答案。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define PDD pair<double,double>
#define ull unsigned long long
using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const int base = ; int n;
int s[N], a[N]; struct SuffixAutomaton {
int last, cur, cnt, ch[N<<][], id[N<<], fa[N<<], dis[N<<], sz[N<<], c[N];
int mx[N<<], mn[N<<];
SuffixAutomaton() {cur = cnt = ;}
void init() {
for(int i = ; i <= cnt; i++) {
memset(ch[i], , sizeof(ch[i]));
sz[i] = c[i] = dis[i] = fa[i] = ;
}
cur = cnt = ;
}
void extend(int c, int id) {
last = cur; cur = ++cnt;
int p = last; dis[cur] = id;
for(; p && !ch[p][c]; p = fa[p]) ch[p][c] = cur;
if(!p) fa[cur] = ;
else {
int q = ch[p][c];
if(dis[q] == dis[p]+) fa[cur] = q;
else {
int nt = ++cnt; dis[nt] = dis[p]+;
memcpy(ch[nt], ch[q], sizeof(ch[q]));
fa[nt] = fa[q]; fa[q] = fa[cur] = nt;
for(; ch[p][c]==q; p=fa[p]) ch[p][c] = nt;
}
}
sz[cur] = ;
}
void getSize(int n) {
for(int i = ; i <= cnt; i++) c[dis[i]]++;
for(int i = ; i <= n; i++) c[i] += c[i-];
for(int i = cnt; i >= ; i--) id[c[dis[i]]--] = i;
for(int i = cnt; i >= ; i--) {
int p = id[i];
sz[fa[p]] += sz[p];
}
}
void solve(int *s, int n) {
memset(mx, , sizeof(int)*(cnt+));
memset(mn, inf, sizeof(int)*(cnt+));
for(int i = , p = ; i <= n; i++) {
p = ch[p][s[i]];
mx[p] = mn[p] = i;
}
for(int i = cnt; i >= ; i--) {
int p = id[i];
mx[fa[p]] = max(mx[fa[p]], mx[p]);
mn[fa[p]] = min(mn[fa[p]], mn[p]);
}
int ans = ;
for(int i = ; i <= cnt; i++) {
ans = max(ans, min(mx[i]-mn[i], dis[i]));
}
if(ans < ) puts("");
else printf("%d\n", ans+);
}
} sam; int main() {
while(scanf("%d", &n) != EOF && n) {
sam.init();
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i < n; i++) s[i] = a[i+]-a[i]+base;
for(int i = ; i < n; i++) sam.extend(s[i], i);
sam.getSize(n-);
sam.solve(s, n-);
}
return ;
} /*
*/