zoukankan      html  css  js  c++  java
  • POJ 3974 最长回文字串(manacher算法)

    题意:给出一个字符串,求出最长回文字串。

    思路:一开始我直接上了后缀数组DC3的解法,然后MLE了。看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法。就去学习了一下,

    /************************以下转自http://funnyxj.blog.163.com/blog/static/20459016020127514231538/********************/

    这个算法要解决的就是一个字符串中最长的回文子串有多长。这个算法可以在O(n)的时间复杂度内既线性时间复杂度的情况下,求出以每个字符为中心的最长回文有多长,
    这个算法有一个很巧妙的地方,它把奇数的回文串和偶数的回文串统一起来考虑了。这一点一直是在做回文串问题中时比较烦的地方。这个算法还有一个很好的地方就是充分利用了字符匹配的特殊性,避免了大量不必要的重复匹配。
    算法大致过程是这样。先在每两个相邻字符中间插入一个分隔符,当然这个分隔符要在原串中没有出现过。一般可以用‘#’分隔。这样就非常巧妙的将奇数长度回文串与偶数长度回文串统一起来考虑了(见下面的一个例子,回文串长度全为奇数了),然后用一个辅助数组P记录以每个字符为中心的最长回文串的信息。P[id]记录的是以字符str[id]为中心的最长回文串,当以str[id]为第一个字符,这个最长回文串向右延伸了P[id]个字符。
    原串: w aa bwsw f d
    新串: # w# a # a # b# w # s # w # f # d #
    辅助数组P: 1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1
    这里有一个很好的性质,P[id]-1就是该回文子串在原串中的长度(包括‘#’)。如果这里不是特别清楚,可以自己拿出纸来画一画,自己体会体会。当然这里可能每个人写法不尽相同,不过我想大致思路应该是一样的吧。
    好,我们继续。现在的关键问题就在于怎么在O(n)时间复杂度内求出P数组了。只要把这个P数组求出来,最长回文子串就可以直接扫一遍得出来了。
    由于这个算法是线性从前往后扫的。那么当我们准备求P[i]的时候,i以前的P[j]我们是已经得到了的。我们用mx记在i之前的回文串中,延伸至最右端的位置。同时用id这个变量记下取得这个最优mx时的id值。(注:为了防止字符比较的时候越界,我在这个加了‘#’的字符串之前还加了另一个特殊字符‘$’,故我的新串下标是从1开始的)
    /********************************************************************************************************************************/

    CODE:

    #include <set>
    #include <map>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <iomanip>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define Max 2505
    #define FI first
    #define SE second
    #define ll long long
    #define PI acos(-1.0)
    #define inf 0x3fffffff
    #define LL(x) ( x << 1 )
    #define bug puts("here")
    #define PII pair<int,int>
    #define RR(x) ( x << 1 | 1 )
    #define mp(a,b) make_pair(a,b)
    #define mem(a,b) memset(a,b,sizeof(a))
    #define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )
    #define N 2000055
    using namespace std;
    
    char s[N] ;
    char str[N] ;
    int rad[N] ;
    int manacher () {
        int len = strlen(s) ;
        int max = 0;
        str[0] = '$';
        str[1] = '#';
        int i = 0  ;
        for (; i < len; i++) {
            str[i * 2 + 2] = s[i];
            str[i * 2 + 3] = '#';
        }
        str[2 * len + 2] = 0;
        for (int i = 1; i < 2 * len + 2 ; i++) {
            rad[i] = 0;
        }
        int id = 0;
        for (i = 1; i < 2 * len + 2; i++) {
            if (max > i)
                rad[i] = min(rad[2 * id - i], rad[id] + id - i) ;
            else
                rad[i] = 1 ;
            while (str[i + rad[i]] == str[i - rad[i]])
                rad[i] ++ ;
            if (rad[i] + i > max) {
                max = rad[i] + i;
                id = i;
            }
        }
        int mx = 0;
        for (i = 1; i < 2 * len + 2 ; i++) {
            if (mx < rad[i] - 1)
                mx = rad[i] - 1;
        }
        return mx;
    }
    
    int main() {
        int ca = 0 ;
        while(scanf("%s",s) != EOF) {
            if(strcmp(s , "END") == 0)break ;
            printf("Case %d: " , ++ ca) ;
            printf("%d
    ",manacher()) ;
        }
        return 0 ;
    }
    


  • 相关阅读:
    【Educational Codeforces Round 101 (Rated for Div. 2) C】Building a Fence
    【Codeforces Round #698 (Div. 2) C】Nezzar and Symmetric Array
    【Codeforces Round #696 (Div. 2) D】Cleaning
    【Codeforces Round #696 (Div. 2) C】Array Destruction
    【Educational Codeforces Round 102 D】Program
    【Educational Codeforces Round 102 C】No More Inversions
    【Good Bye 2020 G】Song of the Sirens
    【Good Bye 2020 F】Euclid's nightmare
    使用mobx入门
    requestAnimationFrame 控制速度模拟setinterval
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3306559.html
Copyright © 2011-2022 走看看