One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
First step: girls will write a long string (only contains lower case) on the paper. For example, "abcde", but 'a' inside is not the real 'a', that means if we define the 'b' is the real 'a', then we can infer that 'c' is the real 'b', 'd' is the real 'c' ……, 'a' is the real 'z'. According to this, string "abcde" changes to "bcdef".
Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.

题意:有一个字符串,先将它进行循环映射,再求其中最长的回文子串,长度至少大于等于2

manacher裸题

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxn=2e5+5;
 7 char s[maxn],t[maxn<<1];
 8 char res[maxn];
 9 int p[maxn<<1];
10 
11 void manacher(){
12     int len=strlen(s),l=0;
13     t[l++]='$';
14     t[l++]='#';
15     for(int i=0;i<len;++i){
16         t[l++]=s[i];
17         t[l++]='#';
18     }
19     t[l]=0;
20     int maxx=0,num=0;
21     for(int i=0;i<l;++i){
22         p[i]=maxx>i?min(p[2*num-i],maxx-i):1;
23         while(t[i+p[i]]==t[i-p[i]])p[i]++;
24         if(i+p[i]>maxx){
25             maxx=i+p[i];
26             num=i;
27         }
28     }
29 }
30 
31 int main(){
32     char c[2];
33     while(scanf("%s%s",c,s)!=EOF){
34         int l=strlen(s);
35         for(int i=0;i<l;++i){
36             s[i]='a'+(s[i]-c[0]+26)%26;
37         }
38         manacher();
39         int pos=0,ans=0;
40         for(int i=1;i<2*l+2;++i){
41             if(p[i]-1>ans){
42                 pos=i;ans=p[i]-1;
43             }
44         }
45         int ans1=0x3f3f3f3f,ans2=0,cnt=0;
46         for(int i=pos-p[pos]+1;i<=pos+p[pos]-1;++i){
47             if(t[i]!='#'){
48                 res[cnt++]=t[i];
49                 if(i<ans1)ans1=i;
50                 if(i>ans2)ans2=i;
51             }
52         }
53         res[cnt]=0;
54         if(p[pos]-1>1){
55             printf("%d %d\n",(ans1-2)/2,(ans2-2)/2);
56             printf("%s\n",res);
57         }
58         else printf("No solution!\n");
59     }
60     return 0;
61 }
View Code