zoukankan      html  css  js  c++  java
  • hdu 3294 Girls' research

    http://acm.hdu.edu.cn/showproblem.php?pid=3294

    Girls' research

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 2443    Accepted Submission(s): 944


    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 <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    int p[2000006];
    char a[2000006],b;
    int main()
    {
        while(cin>>b>>a)
        {
            memset(p,0,sizeof(p));
            int len=strlen(a),id=0,maxlen=0,cnt=0;
            for(int i=len;i>=0;--i)
            {
                a[(i<<1)+1]='#';
                a[(i<<1)+2]=a[i];
            }
            for(int i=2;i<2*len-1;i++)
            {
                if(p[id]+id>i) p[i]=min(p[id*2-i],p[id]+id-i);
                else p[i]=1;
                while(a[i-p[i]]==a[i+p[i]]) ++p[i];
                if(id+p[id]<i+p[i])id=i;
                if(maxlen<p[i]){
                    maxlen=p[i];
                    cnt=i;
                }
            }
            if(maxlen-1<2)puts("No solution!");
            else
            {
                cout<<(cnt-p[cnt]+1)/2<<' '<<(cnt+p[cnt]-3)/2<<endl;
                int k=b-'a';
                for(int i=cnt-p[cnt]+2;i<=cnt+p[cnt]-2;i+=2)
                {
                    cout<<(char)((a[i]-'a'-k+26)%26+'a');
                }
                cout<<endl;
            }
        }
        return 0;
    }

    暴力求解

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    char a[1000006],b;
    int main()
    {
        while(scanf("%c %s",&b,a)!=EOF)
        {
            getchar();
            int len=strlen(a);
            int maxlen=0,k=b-'a',l=0,r=0;
            for(int i=0;i<len;i++)
            {
                for(int j=0;i-j>=0 && i+j<len;j++)
                {
                    if(a[i-j]!=a[i+j])break;
                    if(maxlen<j*2+1)
                    {
                        maxlen=j*2+1;
                        l=i-j;
                        r=i+j;
                    }
                }
                for(int j=0;i-j>=0 && i+j+1<len;j++)
                {
                    if(a[i-j]!=a[i+j+1])break;
                    if(maxlen<j*2+2)
                    {
                        maxlen=j*2+2;
                        l=i-j;
                        r=i+j+1;
                    }
                }
            }
            if(maxlen<2)printf("No solution!
    ");
            else
            {
                cout<<l<<' '<<r<<endl;
                for(int i=l;i<=r;i++)
                    cout<<(char)((a[i]-'a'-k+26)%26+'a');
                cout<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    framework 3 ,4
    C# 线程
    ReportingService错误:配置参数 SharePointIntegrated 被设置为 True,但无法加载 Share Point 对象模型
    C# 打印 word pdf PrintOut方法
    C# 多线程控制控件实例(例程简单,注释详细)
    Windchill MethodServer启动后自动关闭
    Press C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码 (转)
    ExtJs入门
    The transaction log for database 'wcadmin' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.datab
    JS+调用word打印功能实现在Webfrom客户端
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7128307.html
Copyright © 2011-2022 走看看