Manacher Longest palindrome algorithm

Source: Internet
Author: User

Blog has moved, please go to http://gqqnbig.me/read well-formed articles.

This article will step by step constructs the Manacher algorithm, the impatient certainly can not understand! Please practice the following exercises first. Explore the longest palindrome character question 1: A string that is known to be centered in center symmetry for the complete string.
ABCD???   | Center
For
ABCDCBA   |center

Question 2: Answer the question, there are some characters behind ABCDCBA, with Center2 as the center, the maximum symmetric radius [ref] radius is greater than or equal to 1. [/ref] is 7 for the complete string.

According to the symmetric nature of the center2, you can know that the string is

ABCDCBA??? ABCDCb??

And according to the Center2 the maximum symmetric radius is 7, not 8, so C must not be B at the back (identified as b). the symmetry radius of the Center3 is determined! also, the center's center3 of the symmetric point about Center2 is center2*2-center=8*2-3=13. The symmetric radius is center2+7-center3=8+7-13=2.

Question 3: Slightly adjust the above question, if center2 in a, the symmetric radius is 8, the complete string and the symmetric radius of Center3.

Answer: The string is

bABCDCBabcdcbab ?

center3=center2*2-center=7*2-4=10. The symmetric radius is center2+8-center3=7+8-10=5. Is that right?


Looks like the radius is not right! Should be the same as Center 4. What is the problem? Clearly with the same formula as the Title 2 Ah! Because the question 2 is different from the 3 case. Title 2 center left beyond the symmetry range of the Center2, while the title 3 center left does not exceed the Center2 symmetry range. the radius of the Center3 is determined . Title 4: Similarly, center at x, RADIUS is 2,center2 at a, radius is 5. The full string and center3 radius.


Answer: Complement the characters within the Center2 range according to the symmetry of the center2. And because Center2 has a symmetric radius of 5 instead of 6, the 11-bit character of the index is not c.

Ccdxdbab7dxd c One? A???? So Center3 's radius is 2? Not necessarily Oh! Because we only know that the 11-bit character of the index is not C, it may be B, so it matches the 7-bit B of the index, and the symmetric radius reaches 3. Similarly, we do not know what the index 12-bit character is. So, when the left edge of center and Center2 is coincident, we only know the minimum value of the center3 radius. based on these three examples, we have summed up the radius properties of the back characters:
    1. When the Center1 range Zou Center2 range (referred to as Zou), the radius of the Center3 can be calculated as the exact value.
    2. When the Center1 range is included in the Center2 Range (included), the radius of the Center3 is equal to the radius of its symmetric point about the Center2, which is the exact value.
    3. When the left boundary of the Center1 is coincident with the left edge of the Center2 (for short), the center2 radius is greater than or equal to its symmetric point radius on center2, and the value is indeterminate.
In case 1, 2, the exact value is obtained, so even if the center3 is contained in more than one symmetric range, the radius of each symmetric range must be the same. In case 3, the maximum value of the computed value for all symmetric ranges is the minimum value of the center3 radius. Basic code

The basic code is given below. [Code lang= "Java"]public static int getpalindromelength (String s) {if (s.length () = = 0) return 0; StringBuilder sb = new StringBuilder (S.length () * 2 + 1); Sb.setlength (Sb.capacity ()); for (int i = 0; i < s.length (); i+ +) Sb.setcharat (i * 2 + 1, S.charat (i)); s = sb.tostring (); int[] radii = new Int[s.length ()];for (int center = 0; Center &lt ; S.length ()-1; center++)//O (n) {Boolean notsure = true;//checks if center is in the right half of a symmetric region for (int center2 = center-1; center2 >= CENTER/2; center2--) {if (Center2 + radii[center2] > Center)//center in the right half of the symmetric area centered on the center2 {int C1 = Center2 * 2-center;assert C 1 >= 0;if (C1-RADII[C1] < Center2-radii[center2])//Zou, radius ok {Radii[center] = Center2 + radii[center2]-center;no Tsure = False;break;} else if (C1-RADII[C1] > Center2-radii[center2])//included, radius OK {Radii[center] = Radii[c1];notsure = False;break;} else//edge, radius may vary radii[center] = Math.max (RADII[C1], radii[center]);}} if (notsure) {//ccxbabxbabxcc//0123456789012/* * Index 6 x about index 4 of a pairSaid, according to the index 2 of the x symmetrical radius of 1. But actually the X of index 6 is the center of symmetry for the entire string. */int r = radii[center];while (center-r >= 0 && Center + R < s.length () && s.charat (center-r) = = ReadChar (S, center + R)) R++;radii[center] = r;}} int Maxradius = radii[0];for (int i = 0; i < radii.length; i++) Maxradius = Math.max (Maxradius, radii[i]); return (Maxrad IUS-1);} private static char ReadChar (String s, int index)//This method can be used to view the computational complexity of {System.out.println ("read character" + index); return S.charat ( index);} [/code]

Analyze Complexity now , which makes the comparison operation time-consuming. The code passes the Notsure variable and only reads the characters when I is on the symmetric point of the center2. In front of the nature of the analysis is said to the left side, according to Symmetry is also the right side of the edge. After entering the IF (notsure) branch, continue to read the more right character, and then record in radii. Later, you can read it from the radii. It is recommended that you run the code yourself to see that the program will not reread the previously read characters, will not read 012345, and then read 2345. Since the program only reads characters 2n times, the comparison takes place n times, so the time complexity is O (n). Online a lot of code and the above code is not the same, there are IDs, MX, and so on, especially a variable record what the rightmost position, not too understood. If the reader understands the basic code above, then you can look at the following deformation code, the deformation code and online code comparison. The complexity of the Morph code is also O (n). [Code lang= "Java" title= language=]public static int getPalindromeLength2 (String s) {if (s.length () = = 0) return 0; StringBuilder sb = new StringBuilder (S.length () * 2 + 1); Sb.setlength (Sb.capacity ()); for (int i = 0; i < s.length (); i+ +) Sb.setcharat (i * 2 + 1, S.charat (i)); s = sb.tostring (); int[] radii = new Int[s.length ()];int rCenter2 = 0;//The largest cfor in the right boundary (int i = 0; i < s.length ()-1; i++)//O (n) {//Check center is in the right half of a symmetric region if (RCenter2 + radii[rcenter2] > i)//I in Cente R2-centered symmetric area right half {int C1 = RCenter2 * 2-i;assert C1 >= 0;if (C1-RADII[C1] < Rcenter2-radii[rcenter2])//Zou, RADIUS determination RA Dii[i] = RCenter2 + radii[rcenter2]-i;else//not Zou. But if the edges, the radius may changeRadii[i] = radii[c1];} If the right largest C does not include I, then other C will not be included. if (i + radii[i] = = RCenter2 + Radii[rcenter2])//edge {int r = radii[i];while (i-r >= 0 && i + R < s.length ( ) && S.charat (i-r) = = ReadChar (s, i + R)) R++;radii[i] = r;//on the right side, the radius may be greater! if (i + radii[i] > rCenter2 + radii[rcenter2]) RCenter2 = i;}} int Maxradius = radii[0];for (int i = 0; i < radii.length; i++) Maxradius = Math.max (Maxradius, radii[i]); return (Maxrad IUS-1);} [/code]

reference [Citeweb author= "Xiangzhai" url= "https://github.com/xiangzhai/leetcode/blob/master/question/ Longest-palindromic-substring-part-ii.md "title=" longest palindrome substring Part II "publisher=" "date=" 2014-02-14 "accessdate=" 2015-02-06 "]

Manacher Longest palindrome algorithm

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.