hdu 3068 Manacher

hdu 3068
求一个字符串的最长回文长度。
套用Manacher模板即可。

// hdu 3068
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
using namespace std;

#define bll long long
#define dou double
#define For(i,a,b) for (int i=(a),_##i=(b); i<=_##i; ++i)
#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; --i)
#define Mem(a,b) memset(a,b,sizeof(a))
#define Cpy(a,b) memcpy(a,b,sizeof(b))

const int maxn=110000*2+100;
int P[maxn];
char s[maxn],str[maxn];

void Manacher(char s[],char str[],int P[])
{
    int n=strlen(s+1);
    int len=n<<1|1;
    str[0]='$';
    str[1]='#';
    for (int i=1; i<=n; ++i)
    {
        str[i<<1]=s[i];
        str[i<<1|1]='#';
    }
    str[len+1]=0;
    P[1]=1;
    int mid=1,ri=mid+P[mid];
    for (int i=2; i<=len; ++i)
    {
        int j=(i<ri ? min(ri-i,P[mid-(i-mid)]) : 1);
        while(str[i-j]==str[i+j]) ++j;
        P[i]=j;
        if (i+P[i]>ri)
        {
            mid=i;
            ri=mid+P[mid];
        }
    }
}

int main()
{
    for (; scanf("%s",s+1)!=EOF; )
    {
        Manacher(s,str,P);
        int ans=0;
        int len=strlen(s+1)<<1|1;
        for (int i=1; i<=len; ++i)
            ans=max(ans,P[i]-1);
        printf("%d\n",ans);
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • KMP 学习记录 kuangbin专题十六——KMP KMP 学习总结 朴素 KMP 算法 拓展 KMP 算法(E...
    染微言阅读 3,602评论 0 3
  • 最长回文子串——Manacher 算法 1. 问题定义 最长回文字符串问题:给定一个字符串,求它的最长回文子串长度...
    林大鹏阅读 7,727评论 0 6
  • leetcode刷题记录本文记录一下leetcode刷题记录,记录一下自己的解法和心得。 LeetCode Two...
    EarthChen阅读 8,806评论 0 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,391评论 19 139
  • AGlacialSmile阅读 1,579评论 2 4