POJ 3974 Palindrome(Manacher)_palindrome manacher-程序员宅基地

技术标签: 字符串  POJ  

Palindrome

Description

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?" 

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not. 

The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!". 

If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

Input

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity). 

Output

For each test case in the input print the test case number and the length of the largest palindrome. 

Sample Input

abcbabcbabcba
abacacbaaaab
END

Sample Output

Case 1: 13
Case 2: 6
题目大意:给出一个字符串,求它的最长回文子串的长度。

解题思路:Manacher算法的模板题……Manacher算法可以在O(N)时间内求出以各个位置为中心的最长回文子串的长度。(但是这个算法还没有完全搞懂,晚上回去再研究研究)

代码如下:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define EPS 1e-6
#define INF INT_MAX / 10
#define LL long long
#define MOD 100000000
#define PI acos(-1.0)

const int maxn = 2000005;

char s[maxn];
int p[maxn];
int lens;
int manacher()
{
    int mx,id,ans;
    mx = 0,id = 0,ans = 0;
    memset(p,0,sizeof(p));
    for(int i = 1;s[i] != '\0';i++){
        p[i] = mx > i ? std::min(p[2 * id - i], mx - i) : 1;
        while(s[i + p[i]] == s[i - p[i]])
            p[i]++;
        if (i + p[i] > mx){
            mx = i + p[i];
            id = i;
        }
        ans = std::max(ans,p[i]);
    }
    return ans - 1;
}

int main()
{
    char ch;
    int ncase = 0;
    while(1){
        int i = 1,j = 2;
        while((ch = getchar()) != '\n'){
            s[i] = '#';
            s[j] = ch;
            i += 2;
            j += 2;
        }
        lens = strlen(s + 1);
        s[++lens] = '#';
        s[++lens] = '\0';
        s[0] = '$';
        if(strcmp(s + 1,"#E#N#D#") == 0)
            break;
        printf("Case %d: %d\n",++ncase,manacher());
        memset(s,0,sizeof(s));
    }
    return 0;
}


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_26658823/article/details/52143665

智能推荐

python event 事件类 events.py 类_python event csdn-程序员宅基地

文章浏览阅读4.2k次。python event 事件类 events.py 类Locust源码分析之events.py模块(5)https://blog.csdn.net/biheyu828/article/details/84983780eventpy —— Python 事件派发和回调代码库https://zhuanlan.zhihu.com/p/107190607eventpy —— Python 事件..._python event csdn

机器人制作开源方案 | 核酸检测辅助机器人-程序员宅基地

文章浏览阅读1k次,点赞27次,收藏20次。不惧高温、严寒,且精确度高,即使每天都在做重复性的工作,也不会感到劳累。核酸采样模块首先医护人员把棉签放入六自由度的机器臂的末端夹持器里,然后按下采样按键,机械臂末端先夹持住棉签,然后移动到采样区域,再进行核酸采样,采样过后移动到存放采样棉签的试管瓶口的上端,夹持器放下棉签到试管里,最后移动到初始的位置进行下一次采样,程序流程图如下所示。通过先接近,再接近,后采样的策略,本设计采用定点采样,做核酸人员通过一次性采样嘴再机器人采样口等待核酸采样,然后机械臂先接近采样口,再接近人的口腔,最后进行核酸采样。

LPS25HB 寄存器读写程序解读-程序员宅基地

文章浏览阅读935次。文章目录LPS25HB 寄存器读写程序解读1、读写功能的统一接口函数2、设计结构体函数指针来调用统一的读写函数3、与通信方式无关的寄存器读写抽象函数接口LPS25HB 寄存器读写程序解读一般地,芯片公司都会提供芯片驱动的一些驱动代码,以LPS25HB 为例,该Mems工作时,与主MCU通信通过IIC或者SPI的方式进行,从而实现Mems的寄存器的读写。1、读写功能的统一接口函数为了兼容IIC和SPI的通信,我们这里设计两个基于STM32 HAL 库的的读写函数platform_write()、pla_lps25hb

QT工厂配置下位机工具_qt 多个按钮控制下位机-程序员宅基地

文章浏览阅读83次。简单用到了以下这些,都是做上位机比较常用的,记录一下,以后就可以CV大法了!_qt 多个按钮控制下位机

智慧校园电子班牌 智能互联家校互通源码 springboot_智慧校园家校互通-程序员宅基地

文章浏览阅读369次,点赞25次,收藏27次。智慧班牌是专门为学校打造的智能信息展示平台,为学校、教师、学生、家长创造一个学习成长交流的共享平台。_智慧校园家校互通

酸性溶液中HER动力学分析_酸性her-程序员宅基地

文章浏览阅读2.4k次。Bulter-Volmer 方程:i=ic−ia=FAk0[cO(0,t)exp−βF(E−E0′)RT−cR(0,t)exp(1−β)F(E−E0′)RT]i=ic−ia=FAk0[cO(0,t)exp⁡−βF(E−E0′)RT−cR(0,t)exp⁡(1−β)F(E−E0′)RT]i=i_c-i_a=FAk^0 \left [c_O(0,t) \exp \dfrac{-\beta F(E-..._酸性her

随便推点

springboot 与swagger整合出现Unable to infer base url.This is common when using dynamic的解决办法_springboot 集成swagger unable to infer base url. thi-程序员宅基地

文章浏览阅读8.2k次,点赞2次,收藏2次。今天在springboot与swagger整合测试的时候跳出如下所示界面经查资料发现有两种解决办法,1.直接把@EnableSwagger2注解加在主启动类就可以,这样虽然能解决问题,但是这样会扫到使用的框架的接口,这种方法要慎用。2.主启动类加上@ComponentScan("swagger配置类所在包"),以保证配置类被扫描到最后解决问题之后就可以访问 你的配置文..._springboot 集成swagger unable to infer base url. this is common when using d

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFactory-程序员宅基地

文章浏览阅读3.7k次。Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFactoryException in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFactory at org.a..._java.lang.noclassdeffounderror: org/apache/poi/util/poilogfactory

我常用的Latex中文报告模板(一)-程序员宅基地

文章浏览阅读1.6k次。不得不说,使用Latex编写文档效率会提升很多。但是,如果没有好的模板,自己从零开始动手完成一份Latex文档还是得花费不少时间和精力的,所以,为了提高文档和技术报告的编写效率,我为自己准备了以下的Latex模板,并附图如下(注意,在Texlive中需要自己安装黑体(hei)和楷体(kai)等中文字体,具体安装方法我会在下一篇博客中介绍。\documentclass[a4paper, 11..._用latex写报告模版包括但不限于标题、字号和正文

SAP 后继物料简介-程序员宅基地

文章浏览阅读555次。通常情况下当一个物料被停用以后需要更换成另外的一个物料,我们首先想到的就是去更改BOM,的确这也是一种可行的办法,但是这样做会带来BOM的维护人员的工作量,另一个方面是不知道在什么时间点去更改BOM,需要替换的物料在什么时候消耗完。我们可以看到100206的这个物料在被上面的需求消耗一个以后,还剩下库存1PC,是不满足需求的,紧接着根据物料主数据上面的后继物料100208被扣除库存需求1PC。所以在物料永久的替换的业务中,我们可以通过物料主数据上面的后继物料来实现,当物料库存消耗完以后替换成新物料。_sap 后继物料

跨考计算机之路,2014考研心得:零基础跨考之路-程序员宅基地

文章浏览阅读216次。2014考研心得:零基础跨考之路2013-12-02 14:03来源:新东方网作者:我就读于华南农业大学生态学专业,报考的是华南理工大学经济与贸易学院的国民经济学专业。华南理工大学是一所985、211工程院校,而我是跨校跨专业考研,此次考研成绩总分387,政治71,英语一65,数学三117,专业课134。我准备考研是从2012年的三月份,之前一直在徘徊着,不确定要不要跨专业,后来在家人的鼓励下毅然..._生态学跨考计算机

OpenMPI(一) 点对点通信-程序员宅基地

文章浏览阅读579次。为什么80%的码农都做不了架构师?>>> ..._节点如何在 openmpi 中通信

推荐文章

热门文章

相关标签