zoukankan      html  css  js  c++  java
  • kuangbin专题十六 KMP&&扩展KMP HDU3294 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!


    题目比较裸,直接马拉车即可 复习一下长度,子串的求法

    pos 为 回文半径最大的中心
    最大回文半径 = maxr = p[pos]
    最大回文长度 = maxr - 1
    回文起点 = (pos-maxr)/2

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 const int maxn=200010;
     6 char s[maxn],snew[2*maxn];
     7 int p[2*maxn];
     8 char c;
     9 
    10 void manacher(char* s) {
    11     int l=0;
    12     snew[l++]='$';
    13     snew[l++]='#';
    14     for(int i=0;s[i];i++) {
    15         snew[l++]=s[i];
    16         snew[l++]='#';
    17     }
    18     snew[l]='';
    19     int id=0,mx=0,pos,maxlen=-1;
    20     for(int i=0;i<l;i++) {
    21         p[i]=mx>i?min(p[id*2-i],mx-i):1;
    22         while(snew[i-p[i]]==snew[i+p[i]]) p[i]++;
    23         if(i+p[i]>mx) {
    24             mx=i+p[i];
    25             id=i;
    26         }
    27         if(p[i]>maxlen) {
    28             maxlen=p[i];
    29             pos=i;
    30         }
    31     }
    32    // printf("%d**
    ",maxlen);
    33     if(maxlen<=2) printf("No solution!");
    34     else {
    35         printf("%d %d
    ",(pos-maxlen)/2,(pos+maxlen)/2-2);
    36         for(int i=(pos-maxlen)/2;i<=(pos+maxlen)/2-2;i++) {
    37             printf("%c",'a'+(s[i]-c+26)%26);
    38         }
    39         printf("
    ");
    40     }
    41 }
    42 int main() {
    43     //freopen("in","r",stdin);
    44     while(~scanf("%c",&c)) {
    45         scanf("%s
    ",s);
    46         manacher(s);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    在TextBrowser显示中,如何让最新的数据永远出现在第一行或者是在窗口的最后显示信息
    在Qtlabel中显示数字十六进制和十进制都可以
    实现 在子界面的button按下,在主界面的label显示。
    今天在Qt子界面中的Button,转到槽转不过去,报错Qt The class containing 'Ui::MainWindow' could not be found in...
    Qt串口接收使用多个LCD控件显示不同的数据
    Qt图标自定义
    Qt绘制动态曲线
    3.3.2Qt的按钮部件
    Mesh Profile (5)启动配置(配网)
    SiliconLabs EFR32BG 定时器输入捕获和脉宽调制
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/10326296.html
Copyright © 2011-2022 走看看