zoukankan      html  css  js  c++  java
  • 回文串--- Girls' research

    HDU   3294

    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!
     
    Author
    wangjing1111
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  3293 3288 3295 3292 3291 
     
    题意:给了一个字符串,求这个字符串的最长回文串的始末位置,输出起始结束位置,然后根据对应原则('b'是真正的'a', 接着'c' 是真正的 'b', 'd' 是真正的 'c' ……, 'a' 是真正的 'z') ,输出这个最长回文子串;
     
    思路:按照回文串模板算法,得到以每个字符为中心的最长回文串长度,然后可以遍历这个p[]数组,找到最大值即对应坐标,然后便可计算出始末位置了。最后可根据数学求余,得到每个字符的对应字符,输出回文字符子串;
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int N=200010;
    int n,p[2*N];
    char c[5],s[2*N],str[2*N];
    
    void kp()
    {
        int i;
        int mx=0;
        int id;
        for(i=n;str[i]!=0;i++)
        str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba;
        for(i=1;i<n;i++)
        {
            if(mx>i)
                p[i]=min(p[2*id-i],p[id]+id-i);
            else
                p[i]=1;
            for( ;str[i+p[i]]==str[i-p[i]];p[i]++);
            if(p[i]+i>mx)
            {
                mx=p[i]+i;
                id=i;
            }
        }
    }
    
    void init()
    {
        str[0]='$';
        str[1]='#';
        for(int i=0;i<n;i++)
        {
            str[i*2+2]=s[i];
            str[i*2+3]='#';
        }
        n=n*2+2;
        s[n]=0;
    }
    
    int main()
    {
        while(scanf("%s%s",c,s)!=EOF)
        {
            n=strlen(s);
            init();
            kp();
            int ans=0,sta,en=0;
            for(int i=1;i<n;i++)
                if(p[i]>ans)
                en=i,ans=p[i];
                ///cout<<en<<" "<<ans<<endl;
            if(ans-1<2) printf("No solution!
    ");
            else
            {
                 sta=(en-(ans-2))/2-1;
                 en=sta+ans-2;
                 printf("%d %d
    ",sta,en);
                 for(int i=sta;i<=en;i++)
                 {
                     s[i]=(char)((s[i]-'a'-c[0]+'a'+26)%26+'a');
                     printf("%c",s[i]);
                 }
                 cout<<endl;
            }
        }
        return 0;
    }
     
     
  • 相关阅读:
    一个完整的Erlang应用
    Erlang的Web库和框架
    erlang lists
    【erlang 网络编程学习】 分析cowboy acceptor实现
    Mochiweb的设计分析
    Misultin, Mochiweb, Cowboy, NodeJS 及 Tornadoweb测评
    用Mochiweb打造百万级Comet应用,第一部分
    欢迎阅读 Erlang OTP 设计原理文档
    erlang app 文件
    转:DataGridView列的宽度、行的高度自动调整
  • 原文地址:https://www.cnblogs.com/chen9510/p/5401644.html
Copyright © 2011-2022 走看看