题意:就是求一个串的最长回文子串....输出长度。
直接上代码吧,没什么好分析的了。
代码如下:
==============================================================================================================================
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXN = 2e6+7; const int oo = 1e9+37; char s[MAXN]; int p[MAXN]; int Manacher(int len) { int id=0, Max=0; for(int i=2; i<len; i++) { p[i] = 1; if(p[id]+id > i) p[i] = min(p[id*2-i], p[id]+id-i); while(s[i+p[i]] == s[i-p[i]]) p[i]++; if(p[id]+id < p[i]+i) id = i; Max = max(Max, p[i]-1); } return Max; } int main() { int t = 1; while(scanf("%s", s), strcmp(s, "END")) { int N = strlen(s); for(int i=N; i>=0; i--) { s[i+i+2] = s[i]; s[i+i+1] = '#'; } s[0] = '$'; printf("Case %d: %d ", t++, Manacher(N+N+1)); } return 0; }