1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include <cstdio> #include <cstring>
const int N=50000+100; const int INF=1004535809;
int ch[N][176]; int mx[N],right[N]; int parent[N]; int s[N],t[N]; int c[N],id[N]; int siz[N]; int mipos[N],mxpos[N]; int last=1,cnt=1;
template<class T>inline T max(T a,T b){return a<b?b:a;} template<class T>inline T min(T a,T b){return a<b?a:b;}
void insert(int c,int k) { int p=last,np=last=++cnt; mx[np]=mx[p]+1; mipos[np]=mxpos[np]=k; right[np]=1; while (p && !ch[p][c]) ch[p][c]=np,p=parent[p]; if (!p) {parent[np]=1;return;} int q=ch[p][c]; if (mx[q]==mx[p]+1) parent[np]=q; else { int nq=++cnt; mx[nq]=mx[p]+1; memcpy(ch[nq],ch[q],sizeof(ch[q])); while (p && ch[p][c]==q) ch[p][c]=nq,p=parent[p]; parent[nq]=parent[q];parent[q]=parent[np]=nq; } }
void radixsort(int n) { for (int i=1;i<=cnt;++i) ++c[mx[i]]; for (int i=1;i<=n;++i) c[i]+=c[i-1]; for (int i=cnt;i;--i) id[--c[mx[i]]]=i; for (int i=cnt-1;~i;--i) right[parent[id[i]]]+=right[id[i]]; for (int i=cnt-1;~i;--i) { mipos[parent[id[i]]]=min(mipos[parent[id[i]]],mipos[id[i]]); mxpos[parent[id[i]]]=max(mxpos[parent[id[i]]],mxpos[id[i]]); } int ans=0; for (int i=cnt;i;--i) if (mxpos[i]^mipos[i]) ans=max(ans,min(mx[i],mxpos[i]-mipos[i]-1)); if (ans>=4) printf("%d",ans+1); else puts("0"); } int main() { int n; scanf("%d",&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]; for (int i=1;i<=2*n;++i) mxpos[i]=0,mipos[i]=n; for (int i=1;i<=n;++i) insert(s[i]+87,i); radixsort(n); }
|