zoukankan      html  css  js  c++  java
  • 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<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #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<slen;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<<setiosflags(ios::fixed)<<setprecision(2);
        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;i<s.size();i++)s[i]=m[s[i]];
            str="$#";
            for(int i=0;i<s.size();i++)
            {
                str+=s[i];
                str+="#";
            }
         //   cout<<str<<endl;
            slen=str.size();
            memset(p,0,sizeof p);
            manacher();
            int ans=-1,index;
            for(int i=1;i<str.size();i++)
                if(ans<p[i]-1)
                {
                    ans=p[i]-1;
                    index=(i-p[i])/2;
                }
            if(ans>=2)cout<<index<<" "<<index+ans-1<<endl<<s.substr(index,ans)<<endl;
            else cout<<"No solution!"<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Java中String、StringBuffer和StringBuilder的区别
    Map集合学习
    Set集合学习
    List集合学习
    常用集合类简介及线程安全和非线程安全的集合对象
    JQuery入门
    原生Ajax使用
    JavaScript闭包
    Demo:servlet实现图片的上传
    Android Handler 内存泄漏问题
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6835924.html
Copyright © 2011-2022 走看看