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

    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!

    求最大的回文串,起始位置,并输出,如果长度为1,则输出No solution!,每行第一个输出的字母代表a,然后所有字母都相应前移,如果第一个给出的字母是e,代表把e换成a,f换成b,g换成c,d换成z,manacher算法学习题。
    代码:
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    char t[400020];
    char ss[200010];
    int p[400020];
    void manacher(char* s,int change) {
        int d = 0,len = strlen(s);
        t[d ++] = '@';
        t[d ++] = '#';
        for(int i = 0;i < len;i ++) {///字符串字符间及首尾都加上#总字符数为奇数
            t[d ++] = s[i];///字符串中的字符都在偶数位上
            t[d ++] = '#';
        }
        t[d] = 0;
        ///puts(t);
        int mi = 0,r = 0,mlen = 0,mpoint = 0;///mi是能延伸到最右端回文串的中心点,r是对应最右点必然是# 半径包括中心 mlen是最大半径 mpoint对应最大中点
        for(int i = 1;i < d;i ++) {///p[i]记录半径
            p[i] = i < r ? min(p[mi * 2 - i],r - i) : 1;
            while(t[i + p[i]] == t[i - p[i]])p[i] ++;
            if(r < i + p[i])r = i + p[i],mi = i;///更新右边界和中心点
            if(mlen < p[i])mlen = p[i],mpoint = i;///更新最大长度和对应中心点
        }
        mlen --;///mlen本是半径,回文串两边必然是#,也就是说回文串总长是mlen * 2 - 1,#占了mlen,剩下meln - 1是真实回文串长度,所以mlen --
        int st = (mpoint - mlen) / 2,en;///开始位置和结尾位置
        if(mlen > 1) {
            en = mlen + st - 1;
            printf("%d %d
    ",st,en);
            for(int i = st;i <= en;i ++)
                putchar((s[i] - 'a' + change) % 26 + 'a');
            putchar('
    ');
        }
        else puts("No solution!");
    }
    int main() {
        int change;
        char tt[2];
        while(scanf("%s%s",tt,ss) != EOF) {
            change = 26 - (tt[0] - 'a');
            manacher(ss,change);
        }
    }
  • 相关阅读:
    CentOS7中Tomcat的安装和配置
    CentOS7中JDK的安装和配置
    Linux基本指令
    MySQL数据库索引:索引介绍和使用原则
    MongoDB基础篇2:数据库/用户/数据集合的增删改
    MongoDB基础篇1:安装和服务配置
    JSTL fn:replace()函数替换 换行符
    LeetCode440. 字典序的第K小数字
    kubeadm搭建kubernetes-1.13.2集群
    kubernetes(k8s)kubectl使用详解
  • 原文地址:https://www.cnblogs.com/8023spz/p/8885956.html
Copyright © 2011-2022 走看看