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!
题意: 给出一个字符以及一个字符串,字符表示对字符串进行如何的转换,如给出'b',则表示把字符串中的'b'改为'a','c'改为'b'......求改变后的最长回文子串,以及出现的下标(从0开始)。如果不存在长度大于等于2的回文子串,就输出"No solution!"。
分析: manacher算法模板题,字符串如何转化与最长回文子串无关,直接跑manacher求出下标后输出区间内转化后的字符即可。
具体代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char t[200010], s[400010];
int p[400010], len, ansl, ansr;
int manacher()
{
int c = 0, r = 0, ans = 0;
for(int i = 1; i <= 2*len+1; i++)
{
if(i < r)
p[i] = min(r-i, p[2*c-i]);
else
p[i] = 1;
while(s[i+p[i]] == s[i-p[i]])
p[i]++;
if(i+p[i] > r)
{
r = i+p[i];
c = i;
}
if(ans < p[i])
{
ans = p[i];
ansr = i+p[i]-1;
ansl = i-p[i]+1;
}
}
return ans-1;
}
signed main()
{
char ch[2];
while(~scanf("%s", ch))
{
scanf("%s", t+1);
len = strlen(t+1);
for(int i = 1; i <= 2*len; i++)
if(i&1)
s[i] = '#';
else
s[i] = t[i/2];
s[0] = '$';
s[2*len+1] = '#';
s[2*len+2] = '^';
int ans = manacher();
if(ans < 2)
puts("No solution!");
else
{
ansl = (ansl+1)/2-1;
ansr = (ansr-1)/2-1;
printf("%d %d\n", ansl, ansr);
for(int i = ansl+1; i <= ansr+1; i++)
putchar(((t[i]-ch[0])%26+26)%26+'a');
putchar('\n');
}
}
return 0;
}