Manachar’s Algorithm

Longest palindromic substring - Wikipedia  https://en.wikipedia.org/wiki/Longest_palindromic_substring

Longest palindromic substring

From Wikipedia, the free encyclopedia
 
 
 

Jump to navigationJump to search

In computer science, the longest palindromic substring or longest symmetric factor problem is the problem of finding a maximum-length contiguous substring of a given string that is also a palindrome. For example, the longest palindromic substring of "bananas" is "anana". The longest palindromic substring is not guaranteed to be unique; for example, in the string "abracadabra", there is no palindromic substring with length greater than three, but there are two palindromic substrings with length three, namely, "aca" and "ada". In some applications it may be necessary to return all maximal palindromic substrings (that is, all substrings that are themselves palindromes and cannot be extended to larger palindromic substrings) rather than returning only one substring or returning the maximum length of a palindromic substring.

Manacher (1975) invented a linear time algorithm for listing all the palindromes that appear at the start of a given string. However, as observed e.g., by Apostolico, Breslauer & Galil (1995), the same algorithm can also be used to find all maximal palindromic substrings anywhere within the input string, again in linear time. Therefore, it provides a linear time solution to the longest palindromic substring problem. Alternative linear time solutions were provided by Jeuring (1994), and by Gusfield (1997), who described a solution based on suffix trees. Efficient parallel algorithms are also known for the problem.[1]

The longest palindromic substring problem should not be confused with the different problem of finding the longest palindromic subsequence.

Manacher's algorithm[edit]

To find a longest palindrome in a string in linear time, an algorithm may take advantage of the following characteristics or observations about a palindrome and a sub-palindrome:

  1. The left side of a palindrome is a mirror image of its right side.
  2. (Case 1) A third palindrome whose center is within the right side of a first palindrome will have exactly the same length as a second palindrome anchored at the mirror center on the left side, if the second palindrome is within the bounds of the first palindrome by at least one character (not meeting the left bound of the first palindrome). Such as "dacabacad", the whole string is the first palindrome, "aca" in the left side as second palindrome, "aca" in the right side as third palindrome. In this case, the second and third palindrome have exactly the same length.
  3. (Case 2) If the second palindrome meets or extends beyond the left bound of the first palindrome, then the distance from the center of the second palindrome to the left bound of the first palindrome is exactly equal to the distance from the center of the third palindrome to the right bound of the first palindrome.
  4. To find the length of the third palindrome under Case 2, the next character after the right outermost character of the first palindrome would then be compared with its mirror character about the center of the third palindrome, until there is no match or no more characters to compare.
  5. (Case 3) Neither the first nor second palindrome provides information to help determine the palindromic length of a fourth palindrome whose center is outside the right side of the first palindrome.
  6. It is therefore desirable to have a palindrome as a reference (i.e., the role of the first palindrome) that possesses characters farthest to the right in a string when determining from left to right the palindromic length of a substring in the string (and consequently, the third palindrome in Case 2 and the fourth palindrome in Case 3 could replace the first palindrome to become the new reference).
  7. Regarding the time complexity of palindromic length determination for each character in a string: there is no character comparison for Case 1, while for Cases 2 and 3 only the characters in the string beyond the right outermost character of the reference palindrome are candidates for comparison (and consequently Case 3 always results in a new reference palindrome while Case 2 does so only if the third palindrome is actually longer than its guaranteed minimum length).
  8. For even-length palindromes, the center is at the boundary of the two characters in the middle.

Pseudocode[edit]

    given string S
string S' = S with a bogus character (eg. '|') inserted between each character (including outer boundaries)
array P = [0,...,0] // To store the lengths of the palindrome for each center point in S'
// note: length(S') = length(P) = 2 × length(S) + 1 // Track the following indices into P or S'
R = 0 // The next element to be examined; index into S
C = 0 // The largest/left-most palindrome whose right boundary is R-1; index into S
i = 1 // The next palindrome to be calculated; index into P
define L = i − (R − i) // Character candidate for comparing with R; index into S
define i' = C − (i − C) // The palindrome mirroring i from C; index into P while R < length(S'):
If i is within the palindrome at C (Cases 1 and 2):
Set P[i] = P[i'] // note: recall P is initialized to all 0s // Expand the palindrome at i (primarily Cases 2 and 3; can be skipped in Case 1,
// though we have already shown that S'[R] ≠ S'[L] because otherwise the palindrome
// at i' would have extended at least to the left edge of the palindrome at C):
while S'[R] == S'[L]:
increment P[i]
increment R If the palindrome at i extends past the palindrome at C:
update C = i increment i return max(P)

This diverges a little from Manacher's original algorithm primarily by deliberately declaring and operating on R in such a way to help show that the runtime is in fact linear. You can see in the pseudo-code that RC and i are all monotonically increasing, each stepping through the elements in S' and P. (the end condition was also changed slightly to not compute the last elements of P if R is already at the end - these will necessarily have lengths less than P[C] and can be skipped).

The use of S' provides a couple of simplifications for the code: it provides a string aligned to P allowing direct use of the pointers in both arrays and it implicitly enables the inner while-loop to double-increment P[i] and R (because every other time it will be comparing the bogus character to itself).

Notes

Manachar’s Algorithm Tutorials & Notes | Algorithms | HackerEarth https://www.hackerearth.com/practice/algorithms/string-algorithm/manachars-algorithm/tutorial/

Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1 - GeeksforGeeks https://www.geeksforgeeks.org/manachers-algorithm-linear-time-longest-palindromic-substring-part-1/

Manachar’s Algorithm的更多相关文章

  1. Manachar's Algorithm

    1.模板 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAX=21000020; 4 char s[MAX], ...

  2. bzoj 3676 回文串 manachar+hash

    考虑每个回文串,它一定是它中心字母的最长回文串两侧去掉同样数量的字符后的一个子串. 所以我们可以用manachar求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...

  3. BZOJ 2342 & manachar+最优性剪枝

    题意: 求最长回文串,串的两边都是回文串. Solution: manachar预处理然后暴力找... Code: #include <iostream> #include <cst ...

  4. bzoj 3160: 万径人踪灭 manachar + FFT

    3160: 万径人踪灭 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 133  Solved: 80[Submit][Status][Discuss] ...

  5. hdu 3068 最长回文(manachar模板)

    Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等   Input 输 ...

  6. HDU 3294 Girls' research(manachar模板题)

    Girls' researchTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...

  7. HDU 3068 最长回文(manachar算法)

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. Manachar算法详解

    求解最长回文串之Manachar算法 问题类型: 输入一个字符串,求出其中最大的回文子串.子串的含义是:在原串中连续出现的字符串片段. 回文的含义是:正着看和倒着看相同,如abba和yyxyy. 这类 ...

  9. hdu 4513 吉哥系列故事——完美队形II (manachar算法)

    吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) P ...

随机推荐

  1. Blogs禁止页面选中复制功能

    说明:只需要在博客侧边栏公告(支持HTML代码) (支持 JS 代码)里面添加如下代码 /* 在页面定制 CSS 代码处添加如下样式 */ html,body{ moz-user-select: -m ...

  2. 容器编排系统K8s之ConfigMap、Secret资源

    前文我们了解了k8s上的pv/pvc/sc资源的使用和相关说明,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14188621.html:今天我们主要来聊一下 ...

  3. eclipse再见,android studio 新手入门教程(二)项目的导入

    上一篇博客介绍了AS的一些常用设置方法,当工具调教妥当后,自然就要开始项目的开发啦.从零开始新建一个项目,这个简单,不必多说,这篇博客会分享我从旧平台eclipse导入项目到AS的过程,以及遇到的一些 ...

  4. 如何快速学会git

    相信大多数入门者都对git的原理比较恍惚,今天我们来告诉大家如何快速学会git命令. 1.git init 这个命令会在当前目录里创建一个.git目录,也就是初始化本地仓库.git. 如图先创建文件夹 ...

  5. springboot 发布 war jar区别

    fatjar 看下springboot打成jar包后的结构和内容: springboot项目打包的jar 普通jar: 传统jar 通过上面两个图的对比,我们知道这个JAR包与传统JAR包的不同之处在 ...

  6. 简单4步,利用Prometheus Operator实现自定义指标监控

    本文来自Rancher Labs 在过去的文章中,我们花了相当大的篇幅来聊关于监控的话题.这是因为当你正在管理Kubernetes集群时,一切都会以极快的速度发生变化.因此有一个工具来监控集群的健康状 ...

  7. 【并发编程】- 内存模型(针对JSR-133内存模型)篇

    并发编程模型 1.两个关键问题 1)线程之间如何通信 共享内存 程之间共享程序的公共状态,通过写-读内存中的公共状态进行隐式通信 消息传递 程之间没有公共状态,线程之间必须通过发送消息来显式进行通信 ...

  8. Sqoop(一)安装及基本使用

    Sqoop:     1.sqoop从数据库中导入数据到HDFS     2.SQOOP从数据库导入数据到hive     3.sqoop从hive中将数据导出到数据库   sqoop底层还是执行的m ...

  9. 大厂面试官竟然这么爱问Kafka,一连八个Kafka问题把我问蒙了?

    本文首发于公众号:五分钟学大数据 在面试的时候,发现很多面试官特别爱问Kafka相关的问题,这也不难理解,谁让Kafka是大数据领域中消息队列的唯一王者,单机十万级别的吞吐量,毫秒级别的延迟,这种天生 ...

  10. Vim 自动添加脚本头部信息

    每次写脚本还在为忘记添加头部信息啥的烦恼? 按照下面这么做,帮你减轻点烦恼. # 打开配置文件: vim /root/.vimrc # 添加如下信息: autocmd BufNewFile *.sh ...