题目链接:http://poj.org/problem?id=3974
题意:求一给定字符串最长回文子串的长度
思路:直接套模板manacher算法
code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = ;
char Ma[*MAXN];
int Mp[*MAXN]; void Manacher(char s[], int len)
{
int l = ;
Ma[l++] = 'S';
Ma[l++] = '#';
for (int i = ; i < len; ++i)
{
Ma[l++] = s[i];
Ma[l++] = '#';
}
Ma[l] = ;
int mx = ;
int id = ;
for (int i = ; i < l; ++i)
{
Mp[i] = mx > i ? min(Mp[*id-i], mx-i) : ;
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()
{
int cnt = ;
while (scanf("%s", s) != EOF)
{
if (strcmp(s, "END") == ) break;
int len = strlen(s);
Manacher(s, len);
int ans = ;
len = * len + ;
for (int i = ; i < len; ++i) ans = max(ans, Mp[i] - );
printf("Case %d: %d\n",++cnt, ans);
}
return ;
}