1. 程式人生 > >HDU 3068 最長迴文子串(manacher演算法)

HDU 3068 最長迴文子串(manacher演算法)

//
//  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;
}