"Manacher algorithm" oldest palindrome string

Source: Internet
Author: User

"Manacher algorithm"

This algorithm is used to find the longest palindrome substring in a string.

If you take a violent solution to the longest palindrome substring problem, there are probably two ways to think about it: 1. Traverse out all substrings to find the longest palindrome 2. From each character as the center, spread to both sides to see if a palindrome. The second is a little bit smarter than the first, but the overall complexity is O (n^2).

And the manacher algorithm can do O (n) time complexity, O (n) space complexity.

Ideas & Descriptions

Back to the string of characters there is a more troublesome place, is palindrome string have even palindrome and odd palindrome two, respectively, examples of ABBA and ABCBA. This difference may make it easier for us to extend an additional branch of judgment in the program. So the first step of the Manacher algorithm is to preprocess the string, the original string all the characters in the middle plus a special symbol at both ends, so that all possible palindrome can be turned into an odd palindrome, convenient processing:

As ABBA becomes #a#b#b#a#,abcba into #a#b#c#b#a#. In addition, in general practice in order to ensure that the boundary can also be unified, but also extra on the head (in theory, the end of the string can be added, but the tail is certainly a #, it is not big we traverse the end of the round with this # character-based traversal does not do, so that can avoid the tail boundary error) plus a $ or A special symbol that represents the beginning of a string. After this preprocessing, the string can be processed by the manacher algorithm.

Next, the basic idea is definitely to find out with each character as the center, palindrome string the longest can do. Just one by one to traverse too much violence, here can learn from the dynamic planning of a little thought, that is, we can use the existing information (of course, this part of the information needs to be manually saved in the previous analysis process) to more convenient to launch our unknown information.

For example, we can create an array p, for the length of the processed string s,p is set to and s and so on, and the contents of P, is the corresponding to the original S string in the center of the character, the maximum palindrome substring of the radius length. Since all palindrome strings in s are odd palindrome strings, the radius we call refers to the length from the left end of the palindrome string to the center (the center of the calculation). If the radius of the #a#b#a# is 4, the radius of the #a #b#b#a# is 5.

So how do you infer new information based on some existing information? Consider a situation where, if we take the I variable as the subscript to the right, a palindrome string is determined by a gradual right traversal. The center and radius of this substring are two parameters that can be defined. It may be called the center subscript is the ID, called the most right side of the palindrome is labeled MX (this is the subsequent encoding process required to use the two auxiliary variables). After determining the ID and MX We continue to the right over I.

When we come to a new I, if it is still in the MX range, at this point can be noted that there is a point j,j and I about the ID symmetry, because of the palindrome characteristics of id,mx, so the J Palindrome string is very large may be I palindrome string. This may become a certainty only need a condition, that is p[j] of the value, not beyond the range of ID,MX this palindrome string. The following is a stolen diagram that illustrates the layout of i,j,id,mx and MX ' (MX's symmetric points about IDs).

  

So if P[J] > j-mx ', that is, J corresponding Palindrome string has exceeded the scope of MX, at this time how to do? Obviously, in the MX ' to J of this content, due to the id,mx of the palindrome, still can correspond to I to MX this section of the content. As for the post-MX content, you can only go through a character traversal to see if you can match a palindrome.

In general, when I is still less than MX, I should be able to take the value of min (p[j],mx-i), when taking P[j], P[i] is the value of p[j]. When taking Mx-i, Mx-i is still a basic value and needs to be further processed to get the exact value.

Has not been discussed, just said is i<mx situation, if this time has been traversed to more than MX what to do? At this time because there is no existing palindrome string properties can be exploited, so can only be honest to the outside of a character diffusion judgment palindrome. The good point is that the two types of values discussed earlier about I<MX may be (or need to) do this diffusion.

In order to guarantee the continuity of recursion, the ID and MX need to be updated, regardless of the above possibility, after I have been given the palindrome string. It should be noted that the ID and MX are not the properties of the longest palindrome substring we requested last, but just the property of the most right palindrome substring currently traversed.

The attribute that requires the longest palindrome substring we can then maintain a variable similar to Longestinfo, each finding a palindrome string to determine whether it is the longest. It's good to go to this variable after all the loops are over.

Encoding implementation

The following is the python implementation of the Manacher algorithm:

defLongestpalindrome (s):""": Type S:str:rtype:str"""s='$#'+'#'. Join (List (s)) +'#'    #pretreatmentp = [0] *Len (s) longestinfo=[0,0] I,currres,currmax= 1,0,0#since S[0] is $, I take a value starting from 1. ID and MX have a name currres and Currmax, respectively.     whileI <Len (s):ifi > Currmax:#I have exceeded the MX situationP[i] = 1Else:#I did not exceed MX, and then divided into two cases, reflected in the Min functionj = 2*currres-I p[i]= Min (p[j],currmax-i)#at this point p[i] is not necessarily correct, in addition to the decision P[I]=P[J], the other two branches are only given p[i] a benchmark value         whileI-p[i]>0 andI+p[i]<len (s) andS[i-p[i]] = = s[i+P[i]]:#a palindrome test of the outward diffusion of characters            #Note that in order to prevent cross-border access, there are boundary conditionsP[i] + = 1#then P[i] got the final value. The next step is to compare its related properties to the existing currres and Currmax to see if updates are needed .        ifi + P[i] >Currmax:#Update ID and MaxCurrres =I Currmax= i + p[i]-1ifP[i] > Longestinfo[1]:            #Update Final result valuesLongestinfo =I,p[i] I+ = 1Center=Longestinfo[0] Radius= Longestinfo[1]-1#Note that the radius calculates the symmetry center itself, so minus one    returnS[center-radius:center+radius+1].replace ('#',"')#Direct replace removes all auxiliary characters, resulting in the result of the original string. 

"Manacher algorithm" oldest palindrome string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.