HDU 3068 最长回文子串(manacher算法)

来源:互联网 发布:linux 统计登录次数 编辑:程序博客网 时间:2024/05/01 16:48
////  main.cpp//  Richard////  Created by 邵金杰 on 16/9/27.//  Copyright © 2016年 邵金杰. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=110000+100;int p[maxn*2];char s[maxn*2];int main(){    while(scanf("%s",s)!=EOF)    {        int len=(int)strlen(s);        for(int i=len;i>=0;i--){            s[i+i+2]=s[i];            s[i+i+1]='#';        }        int pos=0,MaxRight=0;        for(int i=2;i<2*len+1;i++){            if(p[pos]+pos>i) p[i]=min(p[pos*2-i],p[pos]+pos-i);            else p[i]=1;            while(s[i-p[i]]==s[i+p[i]]) p[i]++;            if(i+p[i]>pos+p[pos]) pos=i;            if(p[i]>MaxRight) MaxRight=p[i];        }        cout<<MaxRight-1<<endl;    }    return 0;}


参考:点击打开链接


0 0
原创粉丝点击