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!
manacher算法求得最长回文串的长度maxr(此时maxr已经在原本Mp【i】的基础上-1)以及该回文串的中心位置pos
则Ma[pos-maxxr+1]~Ma[pos+maxr-1]中去掉#的就是所求串,原位置为(pos-maxr+1)/2-1~(pos+maxr-1)/2-1.
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN=2000005;
char Ma[MAXN*2];
int Mp[MAXN*2];
int min(int a,int b){
if(a>b)
return b;
else
return a;
}
int max(int a,int b){
if(a>b)
return a;
else
return b;
}
int maxr=0,pos;
void Manacher(char s[],int len) {
int l=0;
Ma[l++]='$';
Ma[l++]='#';
for(int i=0; i<len; i++) {
Ma[l++]=s[i];
Ma[l++]='#';
}
Ma[l]=0;
int mx=0,id=0;
for(int i=0; i<l; i++) {
Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;
while(Ma[i+Mp[i]]==Ma[i-Mp[i]])Mp[i]++;
if(i+Mp[i]>mx) {
mx=i+Mp[i];
id=i;
}
if(Mp[i]-1>maxr){ //后加的
maxr=Mp[i]-1;
pos=i;
}
}
}
char s[MAXN];
int main()
{
char kk[10];
while(~scanf("%s%s",&kk,s))
{
maxr=0;
memset(Mp,0,sizeof(Mp));
memset(Ma,0,sizeof(Ma));
int len = strlen(s);
int k = kk[0]-'a';
for(int i=0; i<len; i++)
{
if(s[i]-k<'a')
s[i]=s[i]-k+26;
else
s[i]=s[i]-k;
}
Manacher(s,len);
if(maxr==1){
printf("No solution!\n");
}
else{
int l=pos-maxr+1;
int r=pos+maxr-1;
printf("%d %d\n",l/2-1,r/2-1);
for(int i=l;i<=r;i++){
if(Ma[i]!='#')
printf("%c",Ma[i]);
}
printf("\n");
}
}
return 0;
}