hdu3294 manacher

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!
题意:求改变后的字符串的最长回文
题解:先用map预处理再套一个manacher就行了
(刚开始居然又是tle搞得我很忧伤啊,以为写搓了,改了好几遍终于发现用全局变量就能ac了(不过这是个什么鬼原理))
#include
#include<set>
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define pi acos(-1)
#define ll long long
#define mod 10007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1

using namespace std;

const double g=10.0,eps=1e-9;
const int N=400000+5,maxn=(1<<18)-1,inf=0x3f3f3f3f;

int p[N],slen;
string str;
void manacher()
{
    int mx=0,id;
    for(int i=1;i)
    {
        if(mx>i)p[i]=min(p[2*id-i],mx-i);
        else p[i]=1;
        while(str[i+p[i]]==str[i-p[i]])p[i]++;
        if(i+p[i]>mx)
        {
            mx=i+p[i];
            id=i;
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
 //   cout<
    char op;
    string s;
    while(cin>>op>>s){
        map<char,char>m;
        char k='a';
        for(int i=0;i<26;i++)
        {
            if(op>'z')op-=26;
            m[op++]=k++;
        }
        for(int i=0;im[s[i]];
        str="$#";
        for(int i=0;i)
        {
            str+=s[i];
            str+="#";
        }
     //   cout<
        slen=str.size();
        memset(p,0,sizeof p);
        manacher();
        int ans=-1,index;
        for(int i=1;i)
            if(ans1)
            {
                ans=p[i]-1;
                index=(i-p[i])/2;
            }
        if(ans>=2)cout<" "<1<endl;
        else cout<<"No solution!"<<endl;
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/acjiumeng/p/6835924.html

你可能感兴趣的:(hdu3294 manacher)