manacher
1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int Nmax=200005; 6 char s[Nmax]; 7 char str[Nmax*2+10]; 8 int p[Nmax*2+10]; 9 int hashh[Nmax*2+10]; 10 int id; 11 int maxlen; 12 int len; 13 int start; 14 int endd; 15 void init() 16 { 17 id=0; 18 maxlen=0; 19 int n=strlen(s); 20 str[0]='$'; 21 str[1]='#'; 22 for(int i=0;i<n;i++) 23 { 24 str[i*2+2]=s[i]; 25 hashh[i*2+2]=i; 26 str[i*2+3]='#'; 27 } 28 str[n*2+2]='&'; 29 len=n*2+2; 30 } 31 32 void get() 33 { 34 for(int i=2;i<len-2;i++) 35 { 36 if(id+p[id]>i) 37 p[i]=min(p[id*2-i],p[id]+id-i); 38 else 39 p[i]=1; 40 while(str[i-p[i]] == str[i+p[i]]) 41 p[i]++; 42 if(p[i]+i>p[id]+id) 43 id=i; 44 if(p[i]>maxlen) 45 { 46 maxlen=p[i]; 47 start=hashh[i-p[i]+2]; 48 endd=hashh[i+p[i]-2]; 49 } 50 } 51 } 52 53 int main() 54 { 55 char c; 56 while(scanf("%c",&c)!=EOF) 57 { 58 //printf("c:%c\n",c); 59 getchar(); 60 scanf("%s",s); 61 getchar(); 62 init(); 63 get(); 64 if(maxlen-1>=2) 65 { 66 printf("%d %d\n",start,endd); 67 for(int i=start;i<=endd;i++) 68 { 69 char k=(s[i]-c+26)%26+'a'; 70 printf("%c",k); 71 } 72 printf("\n"); 73 } 74 else 75 printf("No solution!\n"); 76 } 77 return 0; 78 }