Algorithm-Longest palindrome substring (manacher algorithm)

Source: Internet
Author: User

Today in Lintcode to do an interview problem, very simple, the use of conventional methods to calculate a very brief answer, but the interesting is the challenge. Let's look at the question first:

Test instructions

Given a string (assuming a maximum length of 1000) to find its longest palindrome string, you can assume that there is only one longest palindrome that satisfies the condition.

Examples:

The string "Abcdzdcab" is given, and its longest palindrome string is "CDZDC".

Challenge:

O (N2) time complexity algorithm is acceptable, if you can use O (n) algorithm that natural better

The general method is not shown here, the main thing here is to show the Manacher algorithm.

1.Manacher algorithm

First of all, the Manacher algorithm enables the longest palindrome substring to be found at the time complexity of O (n).

(1). An overview of the Manacher algorithm

The Manacher algorithm solves only odd-numbered strings, so the Manacher algorithm uses a clever method to simultaneously resolve strings of odd or even lengths. The Manacher algorithm is implemented by converting strings, for example: The original string: ABC, converted: #a #b#c#, so that the original string length, whether odd or even, will result in a string with an odd length.

Second, the Manacher algorithm usually requires an array (let's suppose, Len Array) to record the maximum length of a string of characters in the original string, in fact, half the length of the longest return string that the character gets in the original.

For example: ABA, after conversion obtained: #a #b#a#,b can get the longest palindrome string is: #a #b#a#, we assume that B is the subscript is I, the longest string to get the first word Poute is left, the last character subscript is right then B corresponds to the value is: Right-i + 1;

At the same time, we can also know that I corresponding to the character of the maximum palindrome substring length is: len[i]-1, for example: The above example, B subscript is 3, so len[3] = 4, then b corresponding Palindrome string aba, the length is exactly len[3]-1 = 3. What is this for? We assume that the original string is oldstring (not converted), the converted string is newstring, assuming that the oldstring length is long, then the length of the newstring is 2 * length + 1, So the resulting string length is definitely an odd number. For the newstring in the first character of the palindrome string, the length is certainly 2 * len[i] 1 (This you can simply give an example test), and, after observation, 2 * Len[i]-1 characters, there must be Len[i] is #, the rest is the normal symbol , because the number of # in all palindrome strings always differs from the number of other symbols by 1.

(2). Calculation of Len Array

First, we calculate len[i from left to right], when we calculate len[i], len[j] has been calculated (0 < J < i).

We assume that the maximum value of the right endpoint of the longest palindrome substring is obtained in the computed characters (the palindrome string is not necessarily the longest palindrome string, but it is required to subscript the right side of the palindrome string maximum), assuming the maximum value is Rightindex, and set the center position of the palindrome substring is centerindex, then there are two cases:

A.I < Rightindex

We assume that Centerindex is the center, and I correspond to J.

What we know is that, with Centerindex as the center, palindrome string can reach Rightindex, then there are two kinds of cases at this time:

If i + Len[j] < P, that is to say, a palindrome string centered on J is inside a palindrome string centered on Centerinde, at this point the palindrome string centered on J is the same as the palindrome string centered on I, why? Because they are inside of a centerindex-centric back-text character string. So what you can get is, len[i] = len[j].

If i + len[j] >= p, that is, the I-centered back string has a portion of the centerindex-centric back-word string outside, at this point the external part of us needs one of our calculations.

B.I > Rightindex

If i > Rightindex, it means that the palindrome string with the center I is not matched at all. So we need a match for one.

2. Code

1  Public Staticstring Longestpalindrome (String string) {2 3         //-----------------------------------4         //Convert String5StringBuilder StringBuilder =NewStringBuilder ();6Stringbuilder.append ("#");7          for(inti = 0; I < string.length (); i++) {8 Stringbuilder.append (String.charat (i));9Stringbuilder.append ("#");Ten         } One         //----------------------------------- A         intRightindex = 0; -         intCenterindex = 0; -         //find the largest in Len the         intAnswer = 0; -         //Answer Center of the largest time -         intindex = 0; -         intLen[] =New int[Stringbuilder.length ()]; +          for(inti = 1; I < stringbuilder.length (); i++) { -             //when Rightindex > I, then we are in Rightindex-i and len[2 * Centerindex-i] (len[j]), get the minimum value +             //because when i + Len[j] < Rightindex, we update len[i] to Len[j] A             //but if i + len[j] >= Rightindex, we will temporarily update len[i] to Rightindex-i, the part of the excess needs one of us to match at             if(Rightindex >i) { -Len[i] = math.min (rightindex-i, len[2 * Centerindex-i]); -}Else { -Len[i] = 1; -             } -             //a single match in             //either the part that is out or the i > Rightindex -              while(I-len[i] >= 0 && i + len[i] < Stringbuilder.length () && Stringbuilder.charat (i-len[i]) = = Strin Gbuilder.charat (i +Len[i])) { tolen[i]++; +             } -             //when Len[i] + i > rightindex, we need to update Centerindex and Rightindex the             //As to why this is done, understand the meaning of Rightindex and Centerindex *             if(Len[i] + i >Rightindex) { $Rightindex = Len[i] +i;Panax NotoginsengCenterindex =i; -             } the             if(Len[i] >answer) { +Answer =Len[i]; Aindex =i; the             } +         } -         //Intercept String $         //why Index-answer + 1, because Len[i]-1 is the original palindrome string length, and answer record is the maximum value in Len $         returnStringbuilder.substring (Index-answer + 1, index + answer). Replace ("#", "" "); -}

Algorithm-Longest palindrome substring (manacher 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.