Manacher Algorithm Summary

Source: Internet
Author: User

Original http://blog.csdn.net/dyx404514/article/details/42061017

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.
Compared with the two algorithms described earlier, the Manacher algorithm has a much narrower application range, but its ideas and expansion KMP algorithms have a lot of common expenses, so here is a brief introduction. The manacher 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
First calculate len[i] from left to right, when calculating len[i], Len[j] (0<=j< i)
The calculation is complete. 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, as shown below:

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, that the I-centered palindrome may extend beyond p, and the part greater than p we have not yet 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].

The 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 that 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 the time to update p, we add a special character before the string T, such as ' $ ', so the string in the algorithm starts at 1.

const int maxn=1000010; char str[maxn];//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 computes the process int manacher (char *st,int len) {int mx=0,ans=0,po=0;//mx is the rightmost word for the current computed palindrome 
         The maximum value 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, match while (St[i-len[i]]==st[i+len[i]]) len[i]++ from the beginning;
             if (LEN[I]+I>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} 

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.