zoukankan      html  css  js  c++  java
  • BZOJ2565:最长双回文串(Manacher)

    Description

    顺序和逆序读起来完全一样的串叫做回文串。比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同)。
    输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分XY,(|X|,|Y|≥1)且XY都是回文串。

    Input

    一行由小写英文字母组成的字符串S

    Output

    一行一个整数,表示最长双回文子串的长度。

    Sample Input

    baacaabbacabb

    Sample Output

    12

    HINT

    样例说明

    从第二个字符开始的字符串aacaabbacabb可分为aacaa与bbacabb两部分,且两者都是回文串。

    对于100%的数据,2≤|S|≤10^5

    Solution

    在manacher的时候顺便统计一下以每个位置为结束/开始的最长回文串最后枚举统计答案就好了

    Code

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #define N (200000+1000)
     5 using namespace std;
     6 
     7 int n,tot,len[N],ans,E[N],S[N];
     8 char s[N],a[N];
     9 
    10 void Manacher()
    11 {
    12     int x,mid=0,maxn=0;
    13     for (int i=1; i<=tot; ++i)
    14     {
    15         if (i>maxn) x=1;
    16         else x=min(maxn-i+1,len[mid*2-i]);
    17         S[i-x+1]=max(S[i-x+1],x*2-1);
    18         E[i+x-1]=max(E[i+x-1],x*2-1);//这里一开始忘加了 
    19         while (s[i+x]==s[i-x])
    20         {
    21             ++x;
    22             S[i-x+1]=max(S[i-x+1],x*2-1);
    23             E[i+x-1]=max(E[i+x-1],x*2-1);
    24         }
    25         len[i]=x;
    26         if (i+x-1>maxn) maxn=i+x-1,mid=i; 
    27     }
    28 }
    29 
    30 int main()
    31 {
    32     scanf("%s",a);
    33     n=strlen(a);
    34     s[++tot]='@'; s[++tot]='#';
    35     for (int i=0; i<n; ++i)
    36         s[++tot]=a[i], s[++tot]='#';
    37     s[++tot]='$';
    38     Manacher();
    39     
    40     for (int i=4; i<tot-2; i+=2)
    41         ans=max(ans,(S[i]+E[i]-1)/2);
    42     printf("%d",ans);
    43 }
  • 相关阅读:
    poj 1417 True Liars(并查集+背包dp)
    CodeForces 760 C. Pavel and barbecue(dfs+思维)
    poj 2912 Rochambeau(枚举+带权并查集)
    lightoj 1245 Harmonic Number (II)(简单数论)
    thinkphp __PUBLIC__的定义 __ROOT__等常量的定义
    HTML5 画布参考
    HTML5 DTD
    HTML5 音频视频
    HTML5 事件
    HTML5 标准属性 NEW:HTML 5 中新的标准属性。 注释:HTML 4.01 不再支持 accesskey 属性:
  • 原文地址:https://www.cnblogs.com/refun/p/9447458.html
Copyright © 2011-2022 走看看