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!
题意:给出一个字符c和一串字符串s,要将s进行转换,转换的方法是将字符c转换成a,其余的也要跟着相应变化。比如字符c为b时,'b'要变成'a','c'要变成'b',。。。。。最后输入最长回文(长度大于等于2)起始位置和终止位置以及回文串。再就是求起始和终止位置有点不太明白。。借鉴了网上的方法。。就暂且先记住吧
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <map>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=2*1e5+5;
int p[maxn*2];
char s[maxn*2];
int n;
int idx;
int manachar()
{
memset(p,0,sizeof(p));
int id=0;
int maxlen=0;
int len=strlen(s);
for(int i=len;i>=0;i--)
{
s[i+i+2]=s[i];
s[i+i+1]='#';
}
s[0]='*';
for(int i=2;i<len+len+1;i++)
{
if(p[id]+id>i)
p[i]=min(p[2*id-i],p[id]+id-i);
else
p[i]=1;
while(s[i-p[i]]==s[i+p[i]])
p[i]++;
if(id+p[id]<i+p[i])
id=i;
if(maxlen<p[i])
{
maxlen=p[i];
idx=i;//后面求起始和终止位置用到
}
}
return maxlen-1;
}
int main()
{
char c[10];
while(scanf("%s%s",c,s)!=EOF)
{
int d=c[0]-'a';
int len=strlen(s);
for(int i=0;i<len;i++)
{
s[i]-=d;
if(s[i]<'a')
s[i]+=26;
}
int ans=manachar();
if(ans<2)
{
printf("No solution!\n");
continue;
}
int L=idx-ans+1;
int R=idx+ans-1;
printf("%d %d\n",(L-2)/2,(R-2)/2);//起始和终止位置
for(int i=L;i<=R;i++)
{
if(s[i]!='#')
printf("%c",s[i]);
}
printf("\n");
}
return 0;
}