Manacher Algorithm Summary

Source: Internet
Author: User

manacher algorithm

The algorithm summarizes the third bomb manacher algorithm, the previous two string phase algorithm--KMP and expand KMP, this time to summarize a string algorithm, Manacher algorithm, I used to call him "horse-drawn Car" algorithm.

relative tothe two previously describedalgorithm,ManacherThe application of the algorithm is much narrower, but its thought and expansion KMPthe algorithm has a lot of common expenses, so let's introduce it here. ManacherThe algorithm is a linear algorithm that finds the longest palindrome substring of a string.

Before introducing the algorithm, first introduce what is palindrome string, so-called palindrome string, simple is to read and reverse read is the same string, such as Abba,noon, and so on, a string of the longest palindrome substring is the substring of this string, is the longest one of palindrome strings.

The simplest algorithm for calculating the longest string of strings is to enumerate each substring of the string, and to determine whether the substring is a palindrome, the time complexity of the algorithm is O (n^3), obviously unsatisfactory, a slightly optimized algorithm is the midpoint of the enumeration palindrome, here to be divided into two cases, One is the length of the palindrome string is odd, the other is the case of palindrome string length is even, the enumeration midpoint to determine whether it is palindrome string, so that the time complexity of the algorithm can be reduced to O (n^2), but when the large n is still not satisfactory, The Manacher algorithm can find the longest string of strings in linear time complexity, and achieves the theoretical lower bound.

principle and implementation of 1.Manacher algorithm

The following describes the principles and procedures of the manacher algorithm.

First, the Manacher algorithm provides a clever way to consider the length of a palindrome string and the length of an even-numbered palindrome together, the practice is to insert a delimiter in the middle of each adjacent two characters of the original string, and to add a delimiter at the beginning and end, the requirements of the delimiter is not present in the original string, In general, you can use the # number. Here's an example:


(1) Len array Introduction and properties

The Manacher algorithm uses an auxiliary array of len[i] to denote the length of the most right-most-to-t[i] of the longest text string centered on the character T[i], such as T[i], then t[l,r.

For the above example, you can conclude that the Len[i] array is:



The Len array has a property, that is, len[i]-1 is the length of the palindrome substring in the original string s, as proof, first in the converted string T, all the length of the text string is odd, then for the t[i] as the center of the longest back text string, its length is 2*len[i]-1, After observation, all of the palindrome string in T, where the number of separators must be more than the number of other characters 1, that is, there are len[i] separators, the remaining len[i]-1 characters from the original string, so the palindrome string in the length of the original strings is len[i]-1.

With this nature, the original problem is translated into all Len[i]. Here's how to find all Len in linear time complexity.

(2) Calculation of Len array

Len[i] is calculated first from left to right, and Len[j] (0<=j<i) is calculated when len[i] is calculated. Set p to the maximum value of the right end point of the longest palindrome string in the previous calculation, and the position of the maximum value is the PO, in two cases:

First case: I<=p

Then find the symmetrical position of I relative to the PO, set to J, then if len[j]<p-i, such as:



Then the J-centered palindrome string must be in the center of the Po-centered palindrome string, and J and I about the position of the PO symmetry, by the definition of palindrome string, a palindrome string in turn is a palindrome string, so I-centered palindrome string at least the same length as the J-centric palindrome, namely len[i]>= LEN[J]. Because Len[j]<p-i, so say i+len[j]<p. by Symmetry Len[i]=len[j].

If len[j]>=p-i, by symmetry, indicates that the palindrome string centered on I may extend beyond p, and that the portion greater than P is not matched, so we will start one match from the p+1 position until the mismatch occurs, updating P and the corresponding PO and len[i].



Second case: I>p

If I is larger than p, indicating that there is no match for the palindrome of the midpoint I, this time, you can only honestly one match, after the completion of the match to update the position of P and the corresponding PO and len[i].



2. Time complexity analysis

The time complexity analysis of the manacher algorithm is similar to the z algorithm, because the algorithm matches only when it encounters a position that has not yet been matched, so that the matched position is no longer matched, so for each position in the T string, only one match is made, so the overall time complexity of the manacher algorithm is O ( n), where n is the length of the T string, and because the length of T is actually twice times that of S, the time complexity is still linear.

The following is the implementation of the algorithm, note that in order to avoid updating p when the result of a cross-border, we add a special character before the string T, such as ' $ ', so the algorithm string is starting from 1.

const int Maxn=1000010;char str[maxn];//The original string char tmp[maxn<<1];//converted string int len[maxn<<1];//convert original string int INIT (    Char *st) {int I,len=strlen (ST);        tmp[0]= ' @ ';//Add a special character at the beginning of the string to prevent cross-border for (i=1;i<=2*len;i+=2) {tmp[i]= ' # ';    TMP[I+1]=ST[I/2];    } tmp[2*len+1]= ' # ';    Tmp[2*len+2]= ' $ ';//string ending with a character to prevent cross-border tmp[2*len+3]=0; Return 2*len+1;//Returns the length of the converted string}//manacher algorithm calculation procedure int Manacher (char *st,int len) {int mx=0,ans=0,po=0;// MX is the maximum value for the currently computed palindrome string rightmost character for (int i=1;i<=len;i++) {if (mx>i) len[i]=min (Mx-i,len[2*po-i]);//In len[          J] and Mx-i take a small else len[i]=1;//if i>=mx, to start from scratch to match while (St[i-len[i]]==st[i+len[i]]) len[i]++;             if (LEN[I]+I&GT;MX)//Jovin calculates the palindrome string right endpoint position greater than MX, to update the value of PO and mx {mx=len[i]+i;         Po=i;     } ans=max (Ans,len[i]); } return ans-1;//returns the maximum value in Len[i]-1 is the longest palindrome substring length of the original string}



Manacher Algorithm Summary

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.