Manacher
可以在(O(n))的时间内求一个字符串的最长回文子串。
例题
HDU - 3068
#include <bits/stdc++.h>
#define FOPI freopen("in.txt", "r", stdin)
#define FOPO freopen("out.txt", "w", stdout)
#define FOR(i,x,y) for (int i = x; i <= y; i++)
#define ROF(i,x,y) for (int i = x; i >= y; i--)
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 100;
char Ma[maxn*2];
int Mp[maxn*2];
void Manacher(char s[], int len)
{
int l = 0;
Ma[l++] = '$';
Ma[l++] = '#';
FOR(i, 0, len-1) Ma[l++] = s[i], Ma[l++] = '#';
Ma[l] = 0;
int mx = 0, id = 0;
FOR(i, 0, l-1)
{
Mp[i] = mx > i ? min(Mp[2*id-i], mx-i) : 1;
while(Ma[i+Mp[i]] == Ma[i-Mp[i]]) Mp[i]++;
if (i+Mp[i] > mx) mx = i+Mp[i], id = i;
}
}
char s[maxn];
int main()
{
while(~scanf("%s", &s))
{
int len = strlen(s);
Manacher(s, len);
int ans = 0;
FOR(i, 0, 2*len+2-1) ans = max(ans, Mp[i]-1);
printf("%d
", ans);
}
}