马拉车用于解决最长回文子串问题,重点是子串,而不是子序列,时间复杂度为O(n)。

解释一下变量的意义:

Len[i]数组去存第i个位置到mx位置的长度

id记录上一次操作的位置(这个操作可以看模板)

mx标记上一次的最长子串的最右端

模板:

 1 void init()  //这个是用来处理字符串的
2 {
3 memset(str,0,sizeof(str));
4 int k=0;
5 str[k++]='$';
6 for(int i=0;i<len;++i)
7 str[k++]='#',str[k++]=s[i];
8 str[k++]='#';
9 len=k;
10 }
11 int manacher() //求最长回文子串
12 {
13 Len[0]=0;
14 int sum=0;
15 int id,mx=0;
16 for(int i=1;i<len;++i)
17 {
18 if(i<mx) Len[i]=min(mx-i,Len[2*id-i]);
19 else Len[i]=1;
20 while(str[i-Len[i]]==str[i+Len[i]]) Len[i]++;
21 if(Len[i]+i>mx)
22 {
23 mx=Len[i]+i;
24 id=i;
25 sum=max(sum,Len[i]);
26 }
27 }
28 return (sum-1);
29 }

当我们要求的以第i个字符为回文字符串的中心的时候,如果i>=mx这个时候没法优化,就是判断(i-1)==(i+1)、(i-2)==(i+2)....一直这样找

看代码就是进行19行、再进行20行

如果i<mx的时候,这个时候

这个时候看一道模板题:

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

这个时候要注意

不知道是这里memset(Len,0,sizeof(Len)); 导致的超时

还是

 1 void init()
2 {
3 memset(str,0,sizeof(str));
4 int k=0;
5 str[k++]='$';
6 for(int i=0;i<strlen(s);++i) 这个strlen导致的
7 str[k++]='#',str[k++]=s[i];
8 str[k++]='#';
9 len=k;
10 }

正确代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<set>
6 using namespace std;
7 const int maxn=3000005;
8 const int INF=0x3f3f3f3f;
9 const int mod=998244353;
10 char str[maxn],s[maxn];
11 int len,Len[maxn];
12 void init()
13 {
14 memset(str,0,sizeof(str));
15 int k=0;
16 str[k++]='$';
17 for(int i=0;i<len;++i)
18 str[k++]='#',str[k++]=s[i];
19 str[k++]='#';
20 len=k;
21 }
22 int manacher()
23 {
24 Len[0]=0;
25 int sum=0;
26 int id,mx=0;
27 for(int i=1;i<len;++i)
28 {
29 if(i<mx) Len[i]=min(mx-i,Len[2*id-i]);
30 else Len[i]=1;
31 while(str[i-Len[i]]==str[i+Len[i]]) Len[i]++;
32 if(Len[i]+i>mx)
33 {
34 mx=Len[i]+i;
35 id=i;
36 sum=max(sum,Len[i]);
37 }
38 }
39 return (sum-1);
40 }
41 int main()
42 {
43 int t=0;
44 while(~scanf("%s",s))
45 {
46 //memset(Len,0,sizeof(Len));
47 if(strcmp("END",s)==0) break;
48 len=strlen(s);
49 init();
50 printf("Case %d: %d\n",++t,manacher());
51 }
52 return 0;
53 }

Manacher算法 & Palindrome的更多相关文章

  1. Palindrome(poj3974)(manacher算法)

    http://poj.org/problem?id=3974 Palindrome Time Limit: 15000MSMemory Limit: 65536K Total Submissions: ...

  2. Palindrome(最长回文串manacher算法)O(n)

     Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  3. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  4. 利用Manacher算法寻找字符串中的最长回文序列(palindrome)

    寻找字符串中的最长回文序列和所有回文序列(正向和反向一样的序列,如aba,abba等)算是挺早以前提出的算法问题了,最近再刷Leetcode算法题的时候遇到了一个(题目),所以就顺便写下. 如果用正反 ...

  5. POJ3974 Palindrome (manacher算法)

    题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...

  6. POJ 3974 Palindrome 字符串 Manacher算法

    http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...

  7. 【Manacher算法】poj3974 Palindrome

    Manacher算法教程:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 模板题,Code 附带注释: #include<cs ...

  8. hdu 3068 最长回文 manacher算法(视频)

    感悟: 首先我要Orz一下qsc,我在网上很难找到关于acm的教学视频,但偶然发现了这个,感觉做的很好,链接:戳戳戳 感觉这种花费自己时间去教别人的人真的很伟大. manacher算法把所有的回文都变 ...

  9. HDU3068 最长回文 Manacher算法

    Manacher算法是O(n)求最长回文子串的算法,其原理很多别的博客都有介绍,代码用的是clj模板里的,写的确实是异常的简洁,现在的我只能理解个大概,下面这个网址的介绍比较接近于这个模板,以后再好好 ...

随机推荐

  1. --safe-user-create

    此参数如果启用,用户将不能用grant语句创建新用户,除非用户有mysql数据库中user表的insert权限, ./mysqld_safe  --safe-user-create & 用-- ...

  2. 关于JDK15的简单理解

    一.为什么要了解JDK15? 2020年9月15日,Oracle官方发布了JDK15版本,及时关注官方的更新动态,可以让我们在日常开发中更合理的选择更加优秀的工具方法,避免使用一些过时的或一些即将被删 ...

  3. Java多线程基础知识笔记(持续更新)

    多线程基础知识笔记 一.线程 1.基本概念 程序(program):是为完成特定任务.用某种语言编写的一组指令的集合.即指一段静态的代码,静态对象. 进程(process):是程序的一次执行过程,或是 ...

  4. 30分钟带你了解「消息中间件」Kafka、RocketMQ

    消息中间件的应用场景 主流 MQ 框架及对比 说明 Kafka 优点 Kafka 缺点 RocketMQ Pulsar 发展趋势 各公司发展 Kafka Kafka 是什么? Kafka 术语 Kaf ...

  5. [已完结]CMU数据库(15-445)实验2-B+树索引实现(下)

    4. Index_Iterator实现 这里就是需要实现迭代器的一些操作,比如begin.end.isend等等 下面是对于IndexIterator的构造函数 template <typena ...

  6. centos7制作U盘启动盘-九五小庞

    一.准备相关软件 1.8G以上U盘 2.UltraISO虚拟光驱(试用版即可)最新版 下载地址:https://cn.ultraiso.net/xiazai.html  点击下载试用 3.CentOS ...

  7. python(paramiko模块的简单使用)

    #通过paramiko模块连接主机运行bash命令 import paramiko hostname = '192.168.88.31' port = 22 username = 'root' pas ...

  8. Windows和Linux下apache-artemis-2.10.0安装配置

     window下安装配置 一.官网下载 http://activemq.apache.org/artemis/download.html 二.百度网盘下载 链接:https://pan.baidu.c ...

  9. Spring AOP介绍与使用

    Spring AOP介绍与使用 AOP:Aspect Oriented Programming 面向切面编程 OOP:Object Oriented Programming 面向对象编程 ​ 面向切面 ...

  10. Any race is a bug. When there is a race, the compiler is free to do whatever it wants.

    https://mp.weixin.qq.com/s/pVJiFdDDKVx707eKL19bjA 谈谈 Golang 中的 Data Race 原创 ms2008 poslua 2019-05-13 ...