Manacher算法(HDU3068 最长回文)

有一篇歪果仁写的关于Manacher的文章,个人感觉写的很好,尤其是配的插图,非常易于理解。

地址:http://articles.leetcode.com/longest-palindromic-substring-part-ii

下面是我趁热做的HDU3068。

/*
Problem :
Status  :

By wf,
*/

#include "algorithm"
#include "iostream"
#include "cstring"
#include "cstdio"
#include "string"
#include "stack"
#include "cmath"
#include "queue"
#include "set"
#include "map"

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1

typedef long long ll;
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=3e6+5;

// http://articles.leetcode.com/longest-palindromic-substring-part-ii

char Ma[maxn];
int Mp[maxn];
int Manacher(char *s,int len)
{
    int l=0;
    Ma[l++]='$';
    Ma[l++]='#';
    for(int i=0;i<len;++i)
    {
        Ma[l++]=s[i];
        Ma[l++]='#';
    }
    int C = 0, R = 0;
    for(int i=0;i<l;++i)
    {
        int i2 = 2*C - i;
        Mp[i] = i<R ? min( Mp[i2],R-i ) : 1;
        while( Ma[ i+Mp[i] ] == Ma[ i-Mp[i] ] ) Mp[i]++;
        if( R<i+Mp[i] )
        {
            R = i+Mp[i];
            C = i;
        }
    }
    int ret = -1;
    for(int i=0;i<l;++i)
    {
        //printf("%d ",Mp[i]);
        ret = max( ret,Mp[i]-1 );
    }
    //printf("
");
    return ret;
}

char str[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    while( scanf("%s",&str)!=EOF )
    {
        //cout<<Manacher(str,strlen(str))<<endl;
        printf("%d
",Manacher( str,strlen(str) ) );
    }
}

  

原文地址:https://www.cnblogs.com/bruce27/p/5500389.html