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 }
  • 相关阅读:
    stm32之波特率、USART
    C语言排序
    【要经常更新】C语言一些题目
    jquery瀑布流布局插件,兼容ie6不支持下拉加载,用于制作分类卡
    jquery等待图片加载完成,再使用函数,使用$(window).load(fn)
    ie6中 object doesn’t support this property or method
    jquery插件中参数未填写错误,火狐不会显示和报错,ie会显示
    flash插件使用外部数据的方法
    织梦在广告(myad)中使用css样式
    织梦改变模板目录
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/10326296.html
Copyright © 2011-2022 走看看