HDU 3068 回文串-Manacher

 

题意链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068

题意:中文题。

思路:Manacher模板题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std; 
const int MAXN=110000+5;
typedef long long int LL;
#define INF 0x3f3f3f3f
char str[MAXN],dstr[MAXN*3]; 
int lenstr,lendstr,p[MAXN*3],ans;
void manacher(){
    memset(p,0,sizeof(p));
    int id=0,mx=0;
    for(int i=1;i<lendstr;i++){
        if(mx>i){
            p[i]=min(p[2*id-i],mx-i);
        }
        else{
            p[i]=1;
        }
        while(dstr[i-p[i]]==dstr[i+p[i]]){
            p[i]++;
        }
        if(p[i]+i>mx){
            mx=p[i]+i;
            id=i;
        }
    }
}
void init(){
    dstr[0]='$';
    dstr[1]='#';
    for(int i=0;i<lenstr;i++){
        dstr[i*2+2]=str[i];
        dstr[i*2+3]='#';
    }
    lendstr=lenstr*2+2;
    dstr[lendstr]='*';
}
int main()
{
    while(~scanf("%s",str)){
        lenstr=strlen(str); 
        init();
        manacher();
        ans=0;
        for(int i=0;i<lendstr;i++){
            ans=max(ans,p[i]);
        }
        printf("%d\n",ans-1);
    }
    return 0;
}

 

posted @ 2016-06-20 17:30  キリト  阅读(116)  评论(0编辑  收藏  举报