zoukankan      html  css  js  c++  java
  • 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
    ",ans-1);
        }
        return 0;
    }
  • 相关阅读:
    waitkey();
    一、数组--删除排序数组中的重复项
    core组件进阶
    opencv数据结构与基本绘图
    highGUI图形用户界面
    合并两个有序链表
    字符串
    内存
    9位运算
    8设备文件
  • 原文地址:https://www.cnblogs.com/kirito520/p/5601222.html
Copyright © 2011-2022 走看看