主要思路是通过对称性简化运算。可以参照https://www.felix021.com/blog/read.php?2040
hdu3068:
#include <bits/stdc++.h>
using namespace std;
char str[150000], s[230000];
int p[230000];
inline int Min(int a, int b)
{ return a<b?a:b; }
inline int Max(int a, int b)
{ return a>b?a:b; }
int main()
{
while (scanf("%s", str+1) != EOF) {
memset(s, 0, sizeof s);
s[0] = '*';
for (int i = 1, len = strlen(str+1), j = 1; i <= len; i++, j += 2)
s[j] = str[i], s[j+1] = '#';
int l = strlen(str+1)*2;
s[l] = '-';
int id = 0, maxl = 0;
for (int i = 1; i <= l; i++) {
if (id+p[id] >= i) p[i] = Min(id+p[id]-i, p[id*2-i]);
else p[i] = 0;
while (s[i+p[i]+1] == s[i-p[i]-1]) p[i]++;
if (i+p[i] >= id+p[id]) id = i;
}
for (int i = 1; i <= l; i++) {
if (s[i] == '#') maxl = Max(maxl, (p[i]+1)/2*2);
else maxl = Max(maxl, p[i]/2*2+1);
}
printf("%d
", maxl);
}
return 0;
}