题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068
分析:参考manacher算法http://wenku.baidu.com/view/3031d2d3360cba1aa811da42.html
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn = 110000 + 10;
char str[maxn];
char str1[maxn*2];
int p[maxn*2];
int n;
void manacher() {
memset(p,0,sizeof(p));
int MaxId=-1;
int id;
for(int i=1; str1[i]; ++i) {
if(MaxId>i) p[i]=min(p[2*id-i], MaxId-i);
else p[i]=1;
while(str1[i+p[i]]==str1[i-p[i]])p[i]++;
if(p[i]+i>MaxId)
MaxId=p[i]+i, id=i;
}
}
void change() {
str1[0]='$';
str1[1]='#';
for(int i=0;i<n; ++i) {
str1[i*2+2]=str[i];
str1[i*2+3]='#';
}
}
int main() {
while(scanf ("%s", str) != EOF) {
memset(str1, '\0', sizeof(str1));
n=strlen(str);
change();
manacher();
int ma = -1;
for(int i = 0; str1[i]; ++i)
if(p[i]-1>ma)ma=p[i]-1;
printf ("%d\n", ma);
}
return 0;
}