题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068
aaaa abab
4 3
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 200017
int N;
int p[maxn];
char str[maxn], b[maxn];
void init()
{
int i;
for(i = 0; str[i]; i ++)
{
b[2 * i + 1] = '#', b[2 * i + 2] = str[i];
}
N = 2 * i + 1;
b[0] = '$', b[N] = b[N + 1] = '#';
}
void solve()
{
int i, id, max = 0, ans = 0;
for(i = 1; i <= N; i ++)
{
p[i] = i < max ? std::min(max - i, p[2 * id - i]) : 1;
while(b[i + p[i]] == b[i - p[i]]) ++ p[i];
if(i + p[i] > max) max = i + p[i], id = i;
if((p[i]-1)>ans) ans=p[i]-1;
}
printf("%d\n", ans);
}
int main()
{
while(~scanf("%s", str))
{
init();
solve();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u012860063/article/details/46957053