老年稳退役选手...
一道水题调试了半个小时...
容易想到manancher处理回文串。
就联想到该题每一个点,该点作为一个回文串的左右端点最长的长度。
然后正向扫一遍即可.
c++代码如下:
#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x; i <= y;++ i)
#define repd(i,x,y) for(register int i = x; i >= y;-- i)
using namespace std;
const int N = 2e5 + 500;
int p[N],id,maxlen,ans,rs[N],ls[N];
char s[N];
int main()
{
scanf("%s",s + 1);
int n = strlen(s + 1);
repd(i,n,1)
s[i<<1] = s[i],s[i<<1|1] = '#';
n = n << 1;
s[1] = '#';s[0] = '$';
rep(i,2,n)
{
if(id + maxlen > i) p[i] = min(id + maxlen - i,p[2*id - i]);
else p[i] = 1;
while(s[i + p[i]] == s[i - p[i]]) p[i]++;
ls[i - p[i] + 2] = max(ls[i - p[i] + 2],p[i] - 1);
rs[i + p[i] - 2] = max(rs[i + p[i] - 2],p[i] - 1);
if(p[i] + i > id + maxlen) id = i,maxlen = p[i];
}
ls[0] = 1;rs[n + 1] = 1;
rep(i,1,n) if(!(i&1)) ls[i] = max(ls[i],ls[i-2]-2);
repd(i,n,1) if(!(i&1)) rs[i] = max(rs[i],rs[i+2]-2);
rep(i,1,n) if(!(i&1)) ans = max(rs[i] + ls[i+2],ans);
cout << ans <<endl;
return 0;
}