两个题目的链接分别为:
hiho:http://hihocoder.com/problemset/problem/1032
hdu:acm.hdu.edu.cn/showproblem.php?pid=3068
manacher算法是用来快速的求得一个字符串中最长的回文串:
其思想就是利用顺序求得的前面的回文串的长度确定后面的未知的回文串的长度,减小复杂度的。
有一篇写的比较好的文章:http://blog.csdn.net/xingyeyongheng/article/details/9310555 可以参考
HDU的AC代码:
(稍微修改就可以在hiho里ac掉)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 110005;
const int INF = 0x3f3f3f3f;
char str[2*maxn];
int p[2*maxn];
int main(){
while(~scanf("%s",str)){
memset(p,0,sizeof(p));
int len = strlen(str);
for(int i=len;i>=0;i--){
str[i+i+2] = str[i] ;
str[i+i+1] = '#' ;
}
str[0] = '*' ;
int id = 0 ;
int maxlen = 0 ;
for(int i=2;i<2*len+1;i++){
if(p[id]+id>i)p[i]=min(p[2*id-i],p[id]+id-i) ;
else p[i]=1 ;
while(str[i-p[i]]==str[i+p[i]])p[i]++;///p[i]是以i为中心的回文串的长度
if(id+p[id]<i+p[i])id = i ;///更新最长延伸回文的中心点
if(maxlen<p[i])maxlen=p[i] ;///最大长度
}
printf("%d\n",maxlen-1);
}
return 0;
}