回文串--- Girls' research
Problem Description
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.
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.
Input
Input 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.
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.
Output
Please 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.
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!
Author
wangjing1111
Source
Recommend
题意:给了一个字符串,求这个字符串的最长回文串的始末位置,输出起始结束位置,然后根据对应原则('b'是真正的'a', 接着'c' 是真正的 'b', 'd' 是真正的 'c' ……, 'a' 是真正的 'z') ,输出这个最长回文子串;
思路:按照回文串模板算法,得到以每个字符为中心的最长回文串长度,然后可以遍历这个p[]数组,找到最大值即对应坐标,然后便可计算出始末位置了。最后可根据数学求余,得到每个字符的对应字符,输出回文字符子串;
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; const int N=200010; int n,p[2*N]; char c[5],s[2*N],str[2*N]; void kp() { int i; int mx=0; int id; for(i=n;str[i]!=0;i++) str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba; for(i=1;i<n;i++) { if(mx>i) p[i]=min(p[2*id-i],p[id]+id-i); else p[i]=1; for( ;str[i+p[i]]==str[i-p[i]];p[i]++); if(p[i]+i>mx) { mx=p[i]+i; id=i; } } } void init() { str[0]='$'; str[1]='#'; for(int i=0;i<n;i++) { str[i*2+2]=s[i]; str[i*2+3]='#'; } n=n*2+2; s[n]=0; } int main() { while(scanf("%s%s",c,s)!=EOF) { n=strlen(s); init(); kp(); int ans=0,sta,en=0; for(int i=1;i<n;i++) if(p[i]>ans) en=i,ans=p[i]; ///cout<<en<<" "<<ans<<endl; if(ans-1<2) printf("No solution!\n"); else { sta=(en-(ans-2))/2-1; en=sta+ans-2; printf("%d %d\n",sta,en); for(int i=sta;i<=en;i++) { s[i]=(char)((s[i]-'a'-c[0]+'a'+26)%26+'a'); printf("%c",s[i]); } cout<<endl; } } return 0; }