Manacher algorithm to find palindrome substring

Source: Internet
Author: User
Tags closing tag

This article is introduced to Manacher in detail, and it is easy to read, the original is transferred from: http://blog.csdn.net/pi9nc/article/details/9251455

First, the problem description

Given a known string str[], now want to find a longest palindrome substring (positive and backward sequential reads) within the time complexity of O (n).

Manacher first found that the time complexity of O (n) can be used to solve this problem, so this method is called Manacher algorithm.

Second, the symbol description

Palindrome string including odd-length and even-length, general demand for the time to discuss the situation, manacher this algorithm to do a simple processing, the parity is unified, in order to avoid the index number beyond the array boundary value of the character comparison, you can be processed in the first position of the string (index 0 position) Add a distinguishing character, and at the end of the string, add the closing tag ' \ s ', after processing the string form as shown in the last example. P[] Stores the radius of a palindrome string. Now you need to calculate the value of P[i], then the ID represents the index number that makes the most right-most character of the palindrome substring before the index number I, denoted by the symbol:

Among them, Argmax is the index of the underlying value, in addition to the introduction of symbolic mx,j=2*id-i;

Third, the algorithm steps

Follow the steps to update p[]:

The first step is to initialize the p[0]=0 first.

Step two, when I<strlen (str) Executes the third step, otherwise ends

The third step is to judge the value of the P[id]+id=mx>i if the fourth step is performed for false. Otherwise, the fifth step is performed.

Fourth step, initialize the p[i]=0, and execute while (str[i+p[i]+1] = = Str[i-p[i]-1]) ++p[i];i++, get the desired p[i].

Fifth step, if MX-I>P[J], then take step sixth, otherwise perform step seventh.

Sixth step, P[i]=p[j], at this time has been found P[i], back to the second step.

Seventh step, at this time p[i]>=mx-i, initialize P[i]=mx-i, and execute while (str[i+p[i]+1] = = Str[i-p[i]-1]) ++p[i];i++, get the p[i], back to the second step.

Here are the steps to find the longest palindrome string according to p[]:

The first step is to find the maximum value in p[] and record the value of the maximum p[] and its index number.

In the second step, the length of the maximum substring is reduced by 1 if the start position of the text substring is ' # '.

In the third step, we get the index position of one character of the longest palindrome substring in the original string.

The fourth step is to derive the longest palindrome substring based on the starting index position number and maximum length of the longest palindrome substring.

Iv. principle of the algorithm

Let's explain the third step of update p[], steps sixth and seventh, as well as the others are very understanding.

First of all, the third step is explained, when the p[id]+id=mx>i indicates that I as the center point may exist palindrome substring, then you can initialize the p[i] to the Palindrome string value in the extension of the search palindrome string radius can be increased, save the p[i] from 0 to start searching for some steps.

Description of the sixth step, that is, the situation of mx-i>p[j]. The string can then be represented as:

The bottom red line in the graph is the palindrome substring that precedes the index number I, which makes the rightmost character of the palindrome substring the largest. J Point is I about the symmetry point of the ID, because the red string is a palindrome string, so about J symmetric palindrome substring and about I symmetric palindrome string is exactly the same (two green lines in the picture), while satisfying mx-i>p[j] It is stated that at this time J's palindrome string radius is less than J to MX on the left end of the J symmetric difference, at this time can initialize P[I]=P[J].

Description of the seventh step, that is, the situation of mx-i<=p[j]. The string can then be represented as:

The bottom red line in the diagram is still the one that precedes the index number I, which makes the most rightmost character of the palindrome substring the largest index number of the palindrome substring. J-Point is I about the symmetry point of the ID, because the red string is a palindrome string, so about the J-symmetric palindrome substring and about I symmetric in the MX and MX symmetric points between the palindrome substring is exactly the same (two green lines in the picture), while satisfying mx-i<=p[j] At this time, J of the Palindrome string radius is greater than or equal to J-to-MX on the left endpoint of J symmetric difference, at this time can initialize the p[i]=mx-i, and then the P[i] palindrome sub-string radius to further increase.

Note the steps to find the longest palindrome string according to p[].

If the first character of the longest palindrome substring of the converted string is ' # ', then the maximum length minus 1 is required. In addition, the radius of the palindrome substring of the converted string (that is, the value of p[] plus 1 is the length of the palindrome substring in the original string (which is the relationship of any palindrome strings included).

V. Examples of algorithms

In order to better understand the principle of the algorithm, I give an example here.

str_s = a B a b a;index = 0 1 2 3 4 5 6 7 8 9 12str[] = @ a # b # a # a # b # a ' +  '; p[]   = 0 0 0 2 0 1 5 1 0 2 0  0;

The index of the top row is the ordinal, and str[] is the string to find. P[] Stores the radius of a palindrome string. For example, p[3]=2 at index 3 means str[3] and the left 2 and two characters of the right number constitute a palindrome string that is a#b#a. The following step-by-step procedure calculates the value of this p[].

The first is to initialize the p[0]=0, easy to get p[1]=0, calculate p[2],id=1,mx=1<2, so initialize p[2]=0, next match whether there is a palindrome string, that is, p[2] to the sides of the expansion, check p[2] whether there are equal characters, can be derived p[2] or 0; calculate p[3],id=2,mx=2<3, initialize p[3]=0, same check p[3] on both sides, calculate p[3]=2; next, calculate p[4],id=3,mx=5>4, which can be derived p[4]>=min (p[2*3-4 ],5-4), that is, at this time p[4] The minimum value is 0, then 5-4>p[2*3-4], so p[4]=p[2*3-4]=0; calculate p[5],id=3,mx=5<=5, so initialize p[5]=0, check p[5] on both sides, then calculate p[5]= 1; calculate p[6] with p[5], get p[6]=5; next calculate p[7],id=6,mx=p[6]+6=11>7,p[7]=min (p[2*6-7],11-7) = 1, that is p[6]>=1 at this time mx-i=11-7 =4>=P[2*6-7], so p[7]=p[2*6-7]=1; calculate P[8],id=6,mx=p[6]+6=11>8,p[8]=min (P[2*6-8],11-8) = 0, initialize p[8]=0, check the sides of P[8], Calculate the p[8]=0 at this time, calculate p[9],id=6,mx=p[6]+6=11>9,p[9]=min (p[2*6-9],11-9) = 0, initialize p[9]=0, check p[9] on both sides, then calculate p[9]= 2, the following do similar processing can calculate all the value of p[i].

The above example may not be appropriate, there is no mx-i<=p[2*id-i] situation, but this situation is also according to the steps of the algorithm analysis can be. I'll try to find an example of this situation.

Manacher algorithm to find palindrome strings

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.