很好的讲解
注意两端的字符要不同,同时数组要开大一些
【Manacher】最长回文子串
#include<bits/stdc++.h>
#define REP(i, a, b) for(register int i = (a); i < (b); i++)
#define _for(i, a, b) for(register int i = (a); i <= (b); i++)
using namespace std;const int MAXN = 3e5 + 10;
int p[MAXN], len;
char a[MAXN], s[MAXN];int init()
{a[0] = '|'; a[1] = '#';int j = 2;REP(i, 0, len){a[j++] = s[i];a[j++] = '#';}a[j] = '&'; return j;
}int solve()
{int n = init();int max_len = -1, last_r = 0, id = 0;REP(i, 1, n) {if(i < last_r) p[i] = min(p[2 * id - i], last_r - i);else p[i] = 1;while(a[i + p[i]] == a[i - p[i]]) p[i]++;if(last_r < i + p[i]){last_r = i + p[i];id = i;}max_len = max(max_len, p[i] - 1);}return max_len;
}int main()
{while(~scanf("%s", s)){len = strlen(s);printf("%d\n", solve());}return 0;
}