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.
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.
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.
Sample Input
b babd
a abcd
Sample Output
0 2
aza
No solution!
//马拉车模板
#pragma warning(disable:4996)
#include"iostream"
#include"functional"
#include"algorithm"
#include"cstring"
#include"stack"
#include"cmath"
#include"queue"
#include"vector"
#include"map"
typedef long long int ll;
using namespace std;
const int maxn=110009;
char a[maxn*3+9],s[maxn*3+9],m;
int p[maxn*3+9];
int ans=0;
int len=0,id=0;
int cnt=0;
inline void solve(){
s[cnt++]='$';
s[cnt++]='#';
for(int i=0;i<len;i++){
s[cnt++]=a[i];
s[cnt++]='#';
}
s[cnt]=0;
int r=0,mid=0;
for(int i=1;i<=cnt;i++){
if(r>i) p[i]=min(p[2*mid-i],r-i);
else p[i]=1;
// p[i]=r>i?min(p[2*mid-i],r-mid):1;
while(s[i+p[i]]==s[i-p[i]]) p[i]++;
if(i+p[i]>r) {
r=i+p[i];
mid=i;
}
if(p[i]-1>ans){
ans=p[i]-1;
id=mid;
}
}
}
int main(){
while(cin>>m>>a){
ans=cnt=len=0;
len=strlen(a);
if(!len) break;
solve();
if(ans>=2){
printf("%d %d\n",(id-ans+2)/2-1,(id-ans+2)/2-2+ans);
for(int i=(id-ans+2)/2-1;i<=(id-ans+2)/2-2+ans;i++){
putchar(((a[i]-'a')-(m-'a')+26)%26+'a');
}
cout<<endl;
}
else{
cout<<"No solution!"<<endl;
}
}
}

本文介绍了一个关于寻找最长回文子串的问题,通过特殊算法(如马拉车算法)处理字符串,以找出给定字符串中长度大于等于2的最长回文子串。文章详细描述了输入输出格式,以及如何通过两步操作找到回文子串:首先转换原始字符串,然后查找并输出回文子串的位置和实际内容。
&spm=1001.2101.3001.5002&articleId=105103650&d=1&t=3&u=eae09da500444e45a4d601e1463cefdd)
423

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



