• hdu3294 Girls' research manacher


    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.

    题意:有一个字符串,先将它进行循环映射,再求其中最长的回文子串,长度至少大于等于2

    manacher裸题

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 const int maxn=2e5+5;
     7 char s[maxn],t[maxn<<1];
     8 char res[maxn];
     9 int p[maxn<<1];
    10 
    11 void manacher(){
    12     int len=strlen(s),l=0;
    13     t[l++]='$';
    14     t[l++]='#';
    15     for(int i=0;i<len;++i){
    16         t[l++]=s[i];
    17         t[l++]='#';
    18     }
    19     t[l]=0;
    20     int maxx=0,num=0;
    21     for(int i=0;i<l;++i){
    22         p[i]=maxx>i?min(p[2*num-i],maxx-i):1;
    23         while(t[i+p[i]]==t[i-p[i]])p[i]++;
    24         if(i+p[i]>maxx){
    25             maxx=i+p[i];
    26             num=i;
    27         }
    28     }
    29 }
    30 
    31 int main(){
    32     char c[2];
    33     while(scanf("%s%s",c,s)!=EOF){
    34         int l=strlen(s);
    35         for(int i=0;i<l;++i){
    36             s[i]='a'+(s[i]-c[0]+26)%26;
    37         }
    38         manacher();
    39         int pos=0,ans=0;
    40         for(int i=1;i<2*l+2;++i){
    41             if(p[i]-1>ans){
    42                 pos=i;ans=p[i]-1;
    43             }
    44         }
    45         int ans1=0x3f3f3f3f,ans2=0,cnt=0;
    46         for(int i=pos-p[pos]+1;i<=pos+p[pos]-1;++i){
    47             if(t[i]!='#'){
    48                 res[cnt++]=t[i];
    49                 if(i<ans1)ans1=i;
    50                 if(i>ans2)ans2=i;
    51             }
    52         }
    53         res[cnt]=0;
    54         if(p[pos]-1>1){
    55             printf("%d %d
    ",(ans1-2)/2,(ans2-2)/2);
    56             printf("%s
    ",res);
    57         }
    58         else printf("No solution!
    ");
    59     }
    60     return 0;
    61 }
    View Code
  • 相关阅读:
    Codeforces 787D. Legacy 线段树优化建图+最短路
    Codeforces 1051E. Vasya and Big Integers
    BZOJ3261 最大异或和
    BZOJ3531 SDOI2014 旅行
    洛谷P2468 SDOI 2010 粟粟的书架
    2018 ICPC 焦作网络赛 E.Jiu Yuan Wants to Eat
    HDU6280 From Tree to Graph
    HDU5985 Lucky Coins 概率dp
    (HDU)1334 -- Perfect Cubes (完美立方)
    (HDU)1330 -- Deck (覆盖物)
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592543.html
Copyright © 2020-2023  润新知