Girls’ research
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 6177 Accepted Submission(s): 2296
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!
题目大意:给你一个字符和一个串,第一个字符表示‘a’,就是说例如b abcd 那么这个串会被改成zabc,全部元素向前面移动了一个偏移量,然后a往后面移动,即a=z,如果是c abcd 那么串应该是yzab,以此类推。然后要你求最大的回文串,输出这么串的起点和终点(在未被修改前的)和最大回文串(新串)。
思路:
先对字符串进行预处理,即增/减偏移量。
然后跑一次mancher算法。
然后遍历p[i]数组找最大回文长度。
根据回文对称性输出回文的内容以及在原串起点和终点
提示:在找到最长回文串后输出它在原串时的起点和终点可以通过增加一个数组去记录新串元素在原串中的下标。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=200000;
char ch;
int len1,len2;
char t[maxn*3],s[maxn*3];
int p[maxn*3];
int index[maxn*3];
void init(){
s[0]='@';
s[1]='#';
int cnt=0;
for(int i=0;i<len1;i++){
s[i*2+2]=t[i];
s[i*2+3]='#';
index[i*2+2]=cnt++;//保存新串下标
}
len2=len1*2+2;
s[len2]='$';
}
void mancher(){
p[0]=0;
int id=0,mx=0;
for(int i=1;i<len2;i++){
if(mx>i){
p[i]=min(mx-i,p[2*id-i]);
}else{
p[i]=1;
}
while(s[i+p[i]]==s[i-p[i]])p[i]++;
if(i+p[i]>mx){
mx=i+p[i];id=i;
}
}
}
int main(){
while(scanf(" %c",&ch)!=EOF){
scanf("%s",t);
len1=strlen(t);
for(int i=0;i<len1;i++){
if(t[i]>=ch)
t[i]=t[i]-(ch-'a');
else
t[i]='z'-(ch-t[i])+1;
}
init();
mancher();
char f;
int start=0,maxlen=0;
for(int i=0;i<len2;i++){
if(p[i]>maxlen){
maxlen=p[i];
start=i;
}
}
//printf("%s\n",s);
if(maxlen<=2){
printf("No solution!\n");
}else{
printf("%d %d\n",index[start-maxlen+2],index[start+maxlen-2]);
for(int i=start-maxlen+1;i<start+maxlen;i++){
if(s[i]!='#')
printf("%c",s[i]);
}
printf("\n");
}
}
}