palindrome 回文 /// Manacher算法

判断最长不连续回文

#include <bits/stdc++.h>
using namespace std;
int main()
{
    char ch[1500];
    while(gets(ch))
    {
        int len=strlen(ch),dp[1500],ans=0;
        for(int i=0;i<len;i++)
            ch[i]=tolower(ch[i]),dp[i]=1;
        for(int i=0;i<len;i++)
        {
            int cnt=0;
            for(int j=i-1;j>=0;j--)
            {
                int tmp=dp[j];
                if(ch[i]==ch[j]) dp[j]=cnt+2;
                cnt=max(cnt,tmp);
            }
        }
        for(int i=0;i<len;i++) ans=max(ans,dp[i]);
        printf("%d\n",ans);
    }

    return 0;
}

过程如图

b b a c b b b c a d
1 1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1 1
3 3 1 1 1 1 1 1 1 1
5 3 1 1 2 1 1 1 1 1
5 4 1 1 3 2 1 1 1 1
5 4 1 5 3 2 1 1 1 1
5 4 7 5 3 2 1 1 1 1
5 4 7 5 3 2 1 1 1 1

判断最长连续回文

https://segmentfault.com/a/1190000008484167

Manacher模板

#include<ctype.h>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char s[205],news[505];
int lens[505];
int init()
{
    news[0]=‘$‘,news[1]=‘#‘;
    int j=2;
    for(int i=0;s[i]!=‘\0‘;i++)
        news[j++]=tolower(s[i]),news[j++]=‘#‘;
    news[j]=‘\0‘;
    return j;
}
int manacher()
{
    int len=init(),mx=0,id,maxlen=0;
    for(int i=1;i<len;i++)
    {
        if(i<mx) lens[i]=min(lens[2*id-i],mx-i);
        else lens[i]=1;

        while(news[i-lens[i]]==news[i+lens[i]])
            lens[i]++;

        if(mx<i+lens[i]) id=i,mx=lens[i]+i;
        maxlen=max(maxlen,lens[i]-1);
    }
    return maxlen;
}
int main()
{
    while(gets(s))
    {
        printf("%d\n",manacher());
    }

    return 0;
}

原文地址:https://www.cnblogs.com/zquzjx/p/8824747.html

时间: 2024-08-02 13:51:01

palindrome 回文 /// Manacher算法的相关文章

HDU 3068 最长回文 (manacher算法)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9188    Accepted Submission(s): 3159 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组

[hdu3068 最长回文]Manacher算法,O(N)求最长回文子串

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题意:求一个字符串的最长回文子串 思路: 枚举子串的两个端点,根据回文串的定义来判断其是否是回文串并更新答案,复杂度O(N3). 枚举回文串的对称轴i,以及回文半径r,由i和r可确定一个子串,然后暴力判断即可.复杂度O(N2). 在上一步的基础上,改进判断子串是否是回文串的算法.记fi(r)=(bool)以i为对称轴半径为r的子串是回文串,fi(r)的值域为{0, 1},显然fi(r)是关于r

[hdu3068]最长回文(Manacher算法)

http://acm.hdu.edu.cn/showproblem.php?pid=3068 题目大意:求最长回文串的长度. 解题关键:Manacher算法 引用一个较好的解释 p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1; 可以这么说,这行要是理解了,那么马拉车算法基本上就没啥问题了,那么这一行代码拆开来看就是 如果mx > i, 则 p[i] = min(p[2 * id - i], mx - i) 否则, p[i] = 1 当 mx - i

HDU 3068-最长回文(Manacher算法O(n)求最长回文串)

题目地址:HDU 3068 关于算法的详解:Manacher算法 #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <algorithm> #include <set> #include <queue>

hdu3068最长回文(Manacher算法)

简单来说这是个很水的东西.有点dp的思想吧.推荐两个博客,很详细. http://blog.csdn.net/xingyeyongheng/article/details/9310555 http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 然后以为随便学点然后去复习,结果……呵呵.什么鬼数据,两行之间用空行隔开……没看到一直wa半节课…… hdu3068最长回文 var s1,s2:ansistring; s:array[0..10

HDU 3068 最长回文(manacher算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S 两组case之间由空行隔开(该空行不用处理) 字符串长度len <= 110000 Output 每

Manacher&#39;s algorithm: 最长回文子串算法

Manacher 算法是时间.空间复杂度都为 O(n) 的解决 Longest palindromic substring(最长回文子串)的算法.回文串是中心对称的串,比如 'abcba'.'abccba'.那么最长回文子串顾名思义,就是求一个序列中的子串中,最长的回文串.本文最后用 Python 实现算法,为了方便理解,文中出现的数学式也采用 py 的记法. 在 leetcode 上用时间复杂度 O(n**2).空间复杂度 O(1) 的算法做完这道题之后,搜了一下发现有 O(n) 的算法.可惜

hdu 3068 Manacher算法 O(n)回文子串算法

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3068 关于算法的教程  推荐这个:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824    注意:我推荐的这篇博客里说的那个代码有bug,我觉得没问题,而是博主在用的时候写错了,博主举得反例我都过了 而且hdu 3068也过了 最开始是用的后缀数组,2000ms+ 果断超时............... 看过一遍很快就学会这个算法了:然后A

hihoCoder #1032 : 最长回文子串 [ Manacher算法--O(n)回文子串算法 ]

传送门 #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能分别在这些字符串中找到它们每一个的最长回文子串呢?” 小Ho奇怪的问道:“什么叫做最长回文子串呢?” 小Hi回答道:“一个字符串中连续的一段就是这个字符串的子串,而回文串