• POJ 3974 回文串-Manacher


    题目链接:http://poj.org/problem?id=3974

    题意:求出给定字符串的最长回文串长度。

    思路:裸的Manacher模板题。 

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    using namespace std; 
    const int MAXN=1000000+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()
    {
        int Ca=1;
        while(~scanf("%s",str)){
            if(strcmp(str,"END")==0){
                break;
            }
            lenstr=strlen(str); 
            init();
            manacher();
            ans=0;
            for(int i=0;i<lendstr;i++){
                ans=max(ans,p[i]);
            }
            printf("Case %d: %d
    ",Ca++,ans-1);
        }
        return 0;
    }
  • 相关阅读:
    四则运算WEB版
    最大子数组问题
    四则运算终极版
    软件工程个人作业02
    软件工程概论-构建之法阅读笔记01
    软件工程概论个人作业01
    软件工程概论作业-测试
    123
    【好文转载】凡人修真传-程序员的十个等级
    有趣的网站
  • 原文地址:https://www.cnblogs.com/kirito520/p/5601212.html
Copyright © 2020-2023  润新知