hdu 3068 最长回文 Manacher算法

来源:互联网 发布:淘宝店铺营销推广方式 编辑:程序博客网 时间:2024/06/16 15:28

http://acm.hdu.edu.cn/showproblem.php?pid=3068


#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;int main(){    char s[220005],str[220005];    int p[220005],n;    int ans;    while(~scanf("%s",s))    {        int n=strlen(s);        str[0]='S';        str[1]='#';        for(int i=0;i<n;i++)        {            str[i*2+2]=s[i];            str[i*2+3]='#';        }        n=n*2+2;        str[n]=0;   //     puts(str);///Manacher        int mx=0,id;        for(int i=1;i<n;i++)        {            if(mx>i)                p[i]=min(p[2*id-i] ,p[id]+id-i );            else p[i]=1;            for(;str[i+p[i]]==str[i-p[i]];p[i]++);            if(p[i]+i>mx)            {                mx=p[i]+i;                id=i;            }        }        ans=0;        for(int i=0;i<n;i++)            ans=max(ans,p[i]);        printf("%d\n",ans-1);    }    return 0;}


0 0
原创粉丝点击