hdu 3294 Girls' research
http://acm.hdu.edu.cn/showproblem.php?pid=3294
Girls' research
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2443 Accepted Submission(s): 944
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!
求最长回文字串的左右所处坐标,并输出该回文字串,当然需要预处理一下,刚开始所接受字符为初始化字符a
manacher算法
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <set> #include <map> #include <algorithm> using namespace std; typedef long long ll; int p[2000006]; char a[2000006],b; int main() { while(cin>>b>>a) { memset(p,0,sizeof(p)); int len=strlen(a),id=0,maxlen=0,cnt=0; for(int i=len;i>=0;--i) { a[(i<<1)+1]='#'; a[(i<<1)+2]=a[i]; } for(int i=2;i<2*len-1;i++) { if(p[id]+id>i) p[i]=min(p[id*2-i],p[id]+id-i); else p[i]=1; while(a[i-p[i]]==a[i+p[i]]) ++p[i]; if(id+p[id]<i+p[i])id=i; if(maxlen<p[i]){ maxlen=p[i]; cnt=i; } } if(maxlen-1<2)puts("No solution!"); else { cout<<(cnt-p[cnt]+1)/2<<' '<<(cnt+p[cnt]-3)/2<<endl; int k=b-'a'; for(int i=cnt-p[cnt]+2;i<=cnt+p[cnt]-2;i+=2) { cout<<(char)((a[i]-'a'-k+26)%26+'a'); } cout<<endl; } } return 0; }
暴力求解
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <set> #include <map> #include <algorithm> using namespace std; typedef long long ll; char a[1000006],b; int main() { while(scanf("%c %s",&b,a)!=EOF) { getchar(); int len=strlen(a); int maxlen=0,k=b-'a',l=0,r=0; for(int i=0;i<len;i++) { for(int j=0;i-j>=0 && i+j<len;j++) { if(a[i-j]!=a[i+j])break; if(maxlen<j*2+1) { maxlen=j*2+1; l=i-j; r=i+j; } } for(int j=0;i-j>=0 && i+j+1<len;j++) { if(a[i-j]!=a[i+j+1])break; if(maxlen<j*2+2) { maxlen=j*2+2; l=i-j; r=i+j+1; } } } if(maxlen<2)printf("No solution!\n"); else { cout<<l<<' '<<r<<endl; for(int i=l;i<=r;i++) cout<<(char)((a[i]-'a'-k+26)%26+'a'); cout<<endl; } } return 0; }