#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>usingnamespace std;typedefunsignedlonglong ULL;constint N =2e6+10;constint P =131;char s[N];
ULL hl[N], hr[N], p[N];
ULL get(ULL h[],int l,int r){
return h[r]- h[l -1]* p[r - l +1];}intmain(){
int t =0;while(cin >> s +1,strcmp(s +1,"END")){
int n =strlen(s +1);
n *=2;for(int i = n; i >0; i -=2){
s[i]= s[i /2];
s[i -1]='#';}
p[0]=1;for(int i =1, j = n; i <= n; i++, j--){
hl[i]= hl[i -1]* P + s[i];
hr[i]= hr[i -1]* P + s[j];
p[i]= p[i -1]* P;}int res =0;for(int i =1; i <= n; i++){
int l =0, r =min(i -1, n - i);while(l < r){
int mid =(l + r +1)>>1;if(get(hl, i - mid, i -1)!=get(hr, n -(i + mid)+1, n -(i +1)+1)) r = mid -1;else l = mid;}if(s[i - l]=='#') res =max(res, l);else res =max(res, l +1);}
cout <<"Case "<<++t <<": "<< res << endl;}return0;}
代码2(Manacher)
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#include<vector>#include<queue>usingnamespace std;constint N =1e6+10;char a[2* N];int p[2* N];intManacher(string c){
int l =0;
a[l++]='$', a[l++]='#';for(int i =0; i < c.size(); i++){
a[l++]= c[i];
a[l++]='#';}
a[l]='0';int mx =0;int id =0;int maxlen =-1;for(int i =1; i < l -1; i++){
p[i]= mx > i ?min(p[2* id - i], mx - i):1;while(a[i - p[i]]== a[i + p[i]]){
p[i]++;}if(i + p[i]> mx){
mx = i + p[i];
id = i;}if(p[i]> maxlen){
maxlen = p[i]-1;}}return maxlen;}intmain(){
string s;int t =0;while(cin >> s && s !="END"){
cout <<"Case "<<++t <<": "<<Manacher(s)<< endl;}return0;}