您的位置:首页 > 其它

HDU 3294 Girls' research (manacher模板题)

2016-12-05 17:20 405 查看
题意:

给你一个字符串,对其凯撒加密后,求最长回文串

代码:

#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.

InputInput 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.
OutputPlease 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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: