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

 

转载于:https://www.cnblogs.com/kirito520/p/5601222.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值