Code:
// luogu-judger-enable-o2 #include <cstdio> #include <algorithm> #include <cstring> #define maxn 300005 #define setIO(s) freopen(s".in","r",stdin) using namespace std; char str[maxn]; int n,l[maxn],ans,r[maxn]; struct PAM{ int fail[maxn],ch[maxn][30],len[maxn],tot,ss[maxn],last; void init(){ ss[0] = -1,fail[0] = 1,last = 0,fail[1] = 0; len[0] = 0,len[1] = -1, tot = 1; } int newnode(int x){ len[++tot] = x; return tot; } int getfail(int p,int nn){ while(ss[nn - len[p] - 1] != ss[nn]) p = fail[p]; return p; } void ins(int c,int i){ int p = getfail(last,i); if(!ch[p][c]) { int q = newnode(len[p] + 2); fail[q] = ch[getfail(fail[p],i)][c]; ch[p][c] = q; } last = ch[p][c]; } }A,B; int main(){ //setIO("input"); scanf("%s",str+1),n = strlen(str + 1), A.init(),B.init(); for(int i = 1;i <= n; ++i) A.ss[i] = str[i] - 'a'; for(int i = n;i >= 1; --i) B.ss[n - i + 1] = str[i] - 'a'; for(int i = 1;i <= n; ++i) { A.ins(A.ss[i],i); l[i] = A.len[A.last]; } for(int i = 1;i <= n; ++i) B.ins(B.ss[i],i),r[n - i + 1] = B.len[B.last]; for(int i = 1;i <= n - 1; ++i) ans = max(ans,l[i] + r[i + 1]); printf("%d",ans); return 0; }