kuangbin专题十六 KMP&&扩展KMP HDU3294 Girls' research

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.

InputInput contains multiple cases.
Each case contains two parts, a character and a string, they are separated by one space, the character representing the real 'a' is and the length of the string will not exceed 200000.All input must be lowercase.
If the length of string is len, it is marked from 0 to len-1.OutputPlease execute the operation following the two steps.
If you find one, output the start position and end position of palindromic string in a line, next line output the real palindromic string, or output "No solution!".
If there are several answers available, please choose the string which first appears.Sample Input
b babd
a abcd
Sample Output
0 2
aza
No solution!


题目比较裸,直接马拉车即可 复习一下长度,子串的求法

pos 为 回文半径最大的中心
最大回文半径 = maxr = p[pos]
最大回文长度 = maxr - 1
回文起点 = (pos-maxr)/2

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=200010;
 6 char s[maxn],snew[2*maxn];
 7 int p[2*maxn];
 8 char c;
 9 
10 void manacher(char* s) {
11     int l=0;
12     snew[l++]='$';
13     snew[l++]='#';
14     for(int i=0;s[i];i++) {
15         snew[l++]=s[i];
16         snew[l++]='#';
17     }
18     snew[l]='\0';
19     int id=0,mx=0,pos,maxlen=-1;
20     for(int i=0;i<l;i++) {
21         p[i]=mx>i?min(p[id*2-i],mx-i):1;
22         while(snew[i-p[i]]==snew[i+p[i]]) p[i]++;
23         if(i+p[i]>mx) {
24             mx=i+p[i];
25             id=i;
26         }
27         if(p[i]>maxlen) {
28             maxlen=p[i];
29             pos=i;
30         }
31     }
32    // printf("%d**\n",maxlen);
33     if(maxlen<=2) printf("No solution!");
34     else {
35         printf("%d %d\n",(pos-maxlen)/2,(pos+maxlen)/2-2);
36         for(int i=(pos-maxlen)/2;i<=(pos+maxlen)/2-2;i++) {
37             printf("%c",'a'+(s[i]-c+26)%26);
38         }
39         printf("\n");
40     }
41 }
42 int main() {
43     //freopen("in","r",stdin);
44     while(~scanf("%c",&c)) {
45         scanf("%s\n",s);
46         manacher(s);
47     }
48     return 0;
49 }

猜你喜欢

转载自www.cnblogs.com/ACMerszl/p/10326296.html
今日推荐