Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9418 Accepted Submission(s): 3238
aaaa abab
4 3
原字符串的回文串的长度 == p[i]-1
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define maxn 310000
int p[maxn] ;
char s[maxn] , str[maxn] ;
int init() {
int i = 0 , j = 0 , l = strlen(s) ;
str[j++] = '$' ;
while( i < l ) {
str[j++] = '#' ;
str[j++] = s[i] ;
i++ ;
}
str[j++] = '#' ;
str[j] = '\0' ;
return j ;
}
void Manacer(int l) {
int i , max1 = 0 , id ;
for(i = 1 ; i < l ; i++) {
if( max1 > i )
p[i] = min(p[2*id-i],max1-i) ;
else
p[i] = 1 ;
while( str[i-p[i]] == str[i+p[i]] )
p[i]++ ;
if( p[i]+i > max1) {
max1 = p[i] + i ;
id = i ;
}
}
}
int main() {
int max1 , i , l ;
while( scanf("%s", s) != EOF ) {
l = init() ;
Manacer(l) ;
max1 = 0 ;
for(i = 1 ; i < l ; i++) {
max1 = max(max1,p[i]) ;
}
printf("%d\n", max1-1) ;
}
return 0 ;
}
原文:http://blog.csdn.net/winddreams/article/details/44218843