AcWing 139. 回文子串的最大长度
原题链接
中等
作者:
大大怪0_0
,
2021-04-30 18:11:50
,
所有人可见
,
阅读 275
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef unsigned long long ull;
const int N = 2000010;
ull hl[N], hr[N], p[N];
int P = 131;
char str[N];
ull get(ull h[], int l, int r)
{
return h[r] - h[l - 1] * p[r - l + 1];
}
int main()
{
int t = 1;
while(scanf("%s", str + 1), strcmp(str + 1, "END"))
{
int n = strlen(str + 1);
for(int i = 2 * n; i > 0; i -= 2)
{
str[i] = str[i / 2];
str[i - 1] = 'z' + 1;
}
n = n * 2;
p[0] = 1;
for(int i = 1, j = n; i <= n; i ++, j --)
{
p[i] = p[i - 1] * P;
hl[i] = hl[i - 1] * P + str[i] - 'a' + 1;
hr[i] = hr[i - 1] * P + str[j] - 'a' + 1;
}
int res = 0;
for(int i = 1; i <= n; i ++)
{
int l = 0;
int r = min(i - 1,n - i);
while(l < r)
{
int mid = l + r + 1 >> 1;
if(get(hl, i - mid, i - 1) ==
get(hr, n - (i + mid) + 1, n - (i + 1) + 1))
l = mid;
else r = mid - 1;
}
if(str[i + l] > 'z') res = max(res, l);
else res = max(res, l + 1);
}
cout << "Case " << t++ << ": " << res << endl;
}
return 0;
}