hdu 3294 Girls' research(manacher)

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 然后要你求解码以后的最长回文序列

思路:先manacher处理一下 然后从头到尾解码即可

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int p[400007]; //注意数组的大小 要开2倍 
void manacher(string s){
    string temp="";
    temp+="$#";
    int len=s.length();
    for(int i=0;i<len;i++){
        temp+=s[i];
        temp+="#";
    }
    len=temp.length();
    int po=0; int mx=0;
    for(int i=0;i<len;i++){
        p[i]=mx>i?min(p[2*po-i],mx-i):1;
        while(temp[p[i]+i]==temp[i-p[i]]) p[i]++;
        if(i+p[i]>mx){
            mx=p[i]+i;
            po=i;
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    char real;
    string s;
    while(cin>>real>>s){
        manacher(s);
        int len=s.length();
        int ans=1;
        int sp=-1;
        for(int i=0;i<2*len+2;i++){
            if(ans<p[i]-1){
                ans=p[i]-1;
                sp=(i-p[i])/2;
            }
        }
        if(sp==-1){
            cout<<"No solution!"<<endl;
        }else{
            cout<<sp<<" "<<sp+ans-1<<endl;
            for(int i=sp;i<=sp+ans-1;i++)
                cout<<char((s[i]-real+26)%26+'a');
            cout<<endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wmj6/p/10600128.html