Girls' research
HDU - 3294
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.
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.
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.
b babd a abcd
0 2 aza No solution!
加个记录原来字符串位置的记录数组就可以了,我的做法很笨
code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 200100;
char op;
char s[MAXN];
char News[MAXN<<1];
int len[MAXN<<1];
int index[MAXN<<1];
int init(int n){
int i,cnt = 0,j = 0,p = 0;
News[cnt++] = '@';
index[j++] = -1;
News[cnt++] = '#';
index[j++] = -1;
for(i = 0; i < n; i++){
News[cnt++] = s[i];
index[j++] = p++;
News[cnt++] = '#';
index[j++] = -1;
}
News[cnt++] = '$';
index[j++] = -1;
News[cnt] = '\0';
return cnt-1;
}
int Manacher(int n){
int i;
int mx = 0,po = 0,ans = 0,ansp;
for(i = 0; i < n; i++){
if(mx > i)
len[i] = min(mx-i,len[2*po-i]);
else
len[i] = 1;
while(News[i-len[i]] == News[i+len[i]])
len[i]++;
if(i+len[i] > mx){
mx = i+len[i];
po = i;
}
}
}
int main(){
while(~scanf("%c",&op)){
scanf("%s",s);
getchar();
int i,ansp,ans = 0;
int n = strlen(s);
for(i = 0; i < n; i++){
if(s[i]-op < 0)
s[i] = 26+(s[i]-op)+'a';
else
s[i] = s[i]-op+'a';
}
n = init(n);
Manacher(n);
for(i = 1; i < n; i++){
if(len[i]-1 > ans){
ans = len[i]-1;
ansp = i;
}
}
if(ans < 2)
printf("No solution!\n");
else{
int st = ansp-ans,ed = ansp+ans;
int ss,ee;
for(i = st; i <= ed; i++){
if(News[i] != '#'){
ss = index[i];
break;
}
}
for(i = ed; i >= st; i--){
if(News[i] != '#'){
ee = index[i];
break;
}
}
printf("%d %d\n",ss,ee);
for(i = ss; i <= ee; i++)
printf("%c",s[i]);
printf("\n");
}
}
return 0;
}
本文介绍了一种特殊的字符串操作挑战,即寻找经过特定转换后的最长回文子串。通过对输入字符串进行预处理和使用Manacher算法,文章详细解释了如何高效地找到满足条件的回文串,并给出了完整的C++代码实现。
423

被折叠的 条评论
为什么被折叠?



