Manacher 算法

      Manacher 算法

详见: http://www.cnblogs.com/biyeymyhjob/archive/2012/10/04/2711527.html

本人只是存一个代码

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<time.h>

using namespace std;
typedef long long LL;
const int INF=2e9+1e8;
const int MOD=1e9+7;
const int MAXSIZE=1e6+5;
const double eps=0.0000000001;
void fre()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
}
#define memst(a,b) memset(a,b,sizeof(a))
#define fr(i,a,n) for(int i=a;i<n;i++)

char s[MAXSIZE*2],str[MAXSIZE*2];
int p[MAXSIZE*2],len;

void init()
{
    int k=0;
    str[k++]='$';
    for(int i=0;i<len;i++)
    {
        str[k++]='#';
        str[k++]=s[i];
    }
    str[k++]='#';
    len=k;
}
void Manacher()
{
    init();
    int mx=0,id;    //mx 为 右边界   ,  id 是对称中心
    for(int i=1;i<len;i++)
    {
        if(mx>i) p[i]=min(p[2*id-i],mx-i);
        else p[i]=1;
        while(str[i+p[i]]==str[i-p[i]]) p[i]++;
        if(p[i]+i>mx)
        {
            mx=p[i]+i;
            id=i;
        }
    }
}
int main(int argc,char *argv[])
{
    while(scanf("%s",&s)!=EOF)
    {
        len=strlen(s);
        Manacher();
        int ans=1;
        for(int i=1;i<len;i++) ans=max(ans,p[i]);
        printf("%d
",ans-1);
    }
    return 0;
}

/**************************************************/
/**             Copyright Notice                 **/
/**  writer: wurong                              **/
/**  school: nyist                               **/
/**  blog  : http://blog.csdn.net/wr_technology  **/
/**************************************************/


原文地址:https://www.cnblogs.com/coded-ream/p/7207951.html