#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=110010;
char str[maxn],tmp[maxn<<1];
int len1[maxn<<1];
int init(char *st){
int len=strlen(st);
tmp[0]='@';
for(int i=1;i<=2*len;i+=2){
tmp[i]='#';
tmp[i+1]=st[i/2];
}
tmp[2*len+1]='#';
tmp[2*len+2]='$';
tmp[2*len+3]=0;
return 2*len+1;
}
int Manacher(char *st,int len){
int p=0,ans=0,po=0;
for(int i=1;i<=len;i++){
if(p>i) len1[i]=min(p-i,len1[2*po-i]);
else len1[i]=1;
while(st[i-len1[i]]==st[i+len1[i]]) len1[i]++;
if(len1[i]+i>p){
p=len1[i]+i;
po=i;
}
ans=max(ans,len1[i]);
}
return ans-1;
}
int main(){
while(scanf("%s",str)!=-1){
init(str);
int len=init(str);
int ans=Manacher(tmp,len);
printf("%d\n",ans);
}
return 0;
}
最长回文
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19635 Accepted Submission(s): 7178
Problem Description
给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.
回文就是正反读都是一样的字符串,如aba, abba等
Input
输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000
Output
每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.
Sample Input