#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define LL long long #define Mo (LL)1000000009 #define BASE (LL)233233 #define min(x,y) ((x)<(y)?(x):(y)) LL Hash[1000030],hAsh[1000030],pw[1000030]; LL ans,n; char s[1000030]; LL get_hash(LL x,LL y){return (hAsh[y]-(hAsh[x-1]*pw[y-x+1])%Mo+Mo)%Mo;} int main(){ scanf("%lld\n",&n); for (int i=1;i<=n;i++) s[i]=getchar(); pw[0]=1; for (int i=1;i<=n;i++){ Hash[i]=(Hash[i-1]+s[i])%Mo; hAsh[i]=(hAsh[i-1]*BASE+s[i])%Mo; pw[i]=pw[i-1]*BASE%Mo; } for (int i=n/2,j=0;i>0;i--,j=min(j+2,(n/2-i))){ for (;j>0;j--) if (get_hash(i+1,i+j)==get_hash(n-i-j+1,n-i) && Hash[i+j]-Hash[i]==Hash[n-i]-Hash[n-i-j]) break; if (get_hash(1,i)==get_hash(n-i+1,n) && Hash[i]==Hash[n]-Hash[n-i]) if (ans<i+j) ans=i+j; } printf("%lld\n",ans); return 0; }
|