Girls' research
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4694 Accepted Submission(s): 1750
Problem Description
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!
Author
wangjing1111
Source
2010 “HDU-Sailormoon” Programming Contest
问题描述:输入格式 :字符(ch) +空格+字符串(s)。要先将s进行转换。ch要转换为'a',其他字符也要相应变化,比如ch='b'时。那么'b'就要转换为’a'。‘c'就要转换为 'b','d'要转换为'c',....,'a'要转换为'z'。简单来说,就要s中的每个字母要发生一定的偏移。题目要求转换后的s的最长回文子串的起始和终止位置,并输出子串,如果有多个则输出第一个。如果没有,就输出''No solution!''
解题思路:先将s进行转换,然后使用Manacher算法,具体看程序。
AC的C++程序:
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=400010;
char s[N];
int p[N];
void Manacher(char s[])
{
p[0]=0,s[0]='&';
int l=strlen(s+1);
for(int i=l;i>0;i--)
s[2*i]=s[i];
l=2*l+1;
for(int i=1;i<=l;i+=2)
s[i]='#';
s[l+1]='@';
int id=0,mx=0;
for(int i=1;i<=l;i++)
{
p[i]=i<mx?min(p[2*id-i],mx-i):1;
while(s[i-p[i]]==s[i+p[i]]) p[i]++;
if(i+p[i]>mx)
{
id=i;
mx=i+p[i];
}
}
int pos=0;
for(int i=1;i<=l;i++)
if(p[pos]<p[i])
pos=i;
if(p[pos]-1==1)
printf("No solution!\n");
else{
int t1=pos-p[pos]+1;
if(t1%2==1) t1++;
int t2=pos+p[pos]-1;
if(t2%2==1) t2--;
printf("%d %d\n",t1/2-1,t2/2-1);
for(int i=t1;i<=t2;i++)
if(i%2==0)
printf("%c",s[i]);
printf("\n");
}
}
int main()
{
char c;
while(~scanf(" %c%s",&c,s+1))
{
int d='a'-c;//位移量
int l=strlen(s+1);
for(int i=1;i<=l;i++)
{
s[i]+=d;
if(s[i]<'a')
s[i]+=26;
}
Manacher(s);
}
return 0;
}