转载:https://blog.csdn.net/happyrocking/article/details/82622881
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
/* p【i】:代表以i为中点的回文串长度。
mx:当前回文串的最右端点的位置。
id:当前最靠右端回文串的中点位置。*/
const int N = 20000005;
char str[N];
int p[N];
void manacher(char *s,int len){
p[0] = 1;
int mx = 0 , id = 0;
for(int i = 1 ;i < len ; i ++){
if(mx>i)p[i]=min(p[id*2 - i],mx-i);
else p[i] = 1;
while(s[i+p[i]] == s[i-p[i]])
p[i] ++;
if(i+p[i] > id + p[id]){
id = i;
mx = i +p[i];
}
}
}
int main(){
while(scanf("%s",str)){
int len = strlen(str);
for(int i = len ; i >= 0; i --){
str[(i << 1) + 1] = '#';
str[(i << 1) + 2] = str[i];
}
str[0] = '@';
len = len*2 +2;
manacher(str,len);
int ans = 0;
for(int i = 0 ; i < len ; i++)
ans = max(ans,p[i]-1);
printf("%d\n",ans);
}
return 0;
}