题意:
给你一个字符串,对其凯撒加密后,求最长回文串
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN=400000;
struct Manacher{
char Ma[MAXN*2];
int Mp[MAXN*2];
int Mx[MAXN*2];
int len;
double ave;
int l;
int ans;
int anspos;
void manachar(string s,int ll){
l=0;ans=0;
len=ll;
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++)
{
ave++;
Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;
while(Ma[i+Mp[i]]==Ma[i-Mp[i]]){
Mp[i]++;
ave++;
}
if(i+Mp[i]>mx)
{
mx=i+Mp[i];
id=i;
}
Mx[i]=mx;
if((Mp[i]-1)>ans){
ans=Mp[i]-1;
anspos=i;
}
}
ave/=len;
}
void debug(){
printf("id: ");
for(int i=0;i<l;i++)
printf("%4d",i);
printf("\n");
printf("char: ");
for(int i=0;i<l;i++)
printf("%c ",Ma[i]);
printf("\n");
printf("Mx: ");
for(int i=0;i<l;i++)
printf("%4d",Mx[i]);
printf("\n");
printf("Mp: ");
for(int i=0;i<l;i++)
printf("%4d",Mp[i]);
printf("\n");
printf("ave: %lf",ave);
printf("\n");
}
};
Manacher man;
int main(){
ios::sync_with_stdio(false);
string str;char s;
while(cin>>s>>str){
int len=str.size();
int mov='a'-s;
mov+=26;
mov%=26;
for(int i=0;i<len;i++)
str[i]=(str[i]+mov-'a')%26+'a';
man.manachar(str,len);
if(man.ans>=2){
int st=(man.anspos-man.ans-1)/2,en=st+man.ans-1;
cout<<st<<' '<<en<<endl;
for(int i=st;i<=en;i++)
cout<<str[i];
cout<<endl;
}else{
cout<<"No solution!"<<endl;
}
}
}
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.
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.
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.
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.
b babd a abcd
0 2 aza No solution!