AcWing 139. 回文子串的最大长度
原题链接
中等
作者:
wjie
,
2020-07-07 23:31:52
,
所有人可见
,
阅读 699
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e6 + 5;
char s[N];
int p[N], length;
void trim(string str)
{
length = 0;
s[length++] = '$', s[length++] = '#';
for (int i = 0; i < str.size(); ++i)
{
s[length++] = str[i];
s[length++] = '#';
}
s[length] = '\0';
}
int manacher()
{
int res = 0, id = 0, mx = 0;
for (int i = 0; i < length; ++i)
{
p[i] = i < mx ? min(p[2*id-i], mx-i) : 1;
while (i-p[i] >= 0 && i+p[i] < length && s[i-p[i]] == s[i+p[i]])
p[i]++;
if (mx < i + p[i])
{
mx = i + p[i];
id = i;
}
res = p[i]-1 > res ? p[i]-1 : res;
}
return res;
}
int main()
{
string str;
int T = 0;
while (cin >> str && str.compare("END"))
{
trim(str);
//printf("%s\n", s);
memset(p, 0, sizeof(p));
printf("Case %d: %d\n", ++T, manacher());
}
return 0;
}