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
| #include<bits/stdc++.h> using namespace std; #ifdef LOCAL #define debug(...) fprintf(stdout, ##__VA_ARGS__) #else #define debug(...) void(0) #endif #define int long long inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+ (x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x) #define pb push_back #define fi first #define se second
#define mo 998244353 #define N 1000010 void Mod(int &a) { a%=mo; a+=mo; a%=mo; } int n, m, i, j, k, T, f[N], c[N], fac[N], ans; int l, r; char str[N];
int substr(int a, int b) { return ((c[b]-c[a-1]*fac[b-a+1])%mo+mo)%mo; }
bool cmp(int a, int b, int c, int d) { return substr(a, b) == substr(c, d); }
signed main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif
n=read(); scanf("%s", str+1); ans=0; for(i=fac[0]=1; i<=n; ++i) c[i]=c[i-1]*29+str[i]-'a'+1, Mod(c[i]), fac[i]=fac[i-1]*29%mo; for(i=n/2; i>=1; --i) { l=i; r=n-i+1; f[i]=f[i+1]+2; while(f[i] && (l+f[i]-1>=r-f[i]+1 || !cmp(l, l+f[i]-1, r-f[i]+1, r))) --f[i]; debug("f[%lld][%lld : %lld] = %lld \n", i, l, r, f[i]); } for(i=1; i<=n/2; ++i) if(cmp(1, i, n-i+1, n)) { debug("%d is ok !\n", i); ans=max(ans, i+f[i+1]); } printf("%lld", ans); return 0; }
|