ACM模版
Manacher算法
最长回文子串实例
const int MAXN = 110010;
char A[MAXN * 2];
int B[MAXN * 2];
void Manacher(char s[], int len)
{
int l = 0;
A[l++] = '$';
A[l++] = '#';
for (int i = 0; i < len; i++)
{
A[l++] = s[i];
A[l++] = '#';
}
A[l] = 0;
int mx = 0;
int id = 0;
for (int i = 0; i < l; i++)
{
B[i] = mx > i ? std::min(B[2 * id - i], mx - i) : 1;
while (A[i + B[i]] == A[i - B[i]])
{
B[i]++;
}
if (i + B[i] > mx)
{
mx = i + B[i];
id = i;
}
}
return ;
}
char s[MAXN];
int main(int argc, const char * argv[])
{
while (std::cin >> s)
{
int len = (int)strlen(s);
Manacher(s, len);
int ans = 0;
for (int i = 0; i < 2 * len + 2; i++)
{
ans = std::max(ans, B[i] - 1);
}
std::cout << ans << std::endl;
}
return 0;
}