Find the longest palindrome substring: manacher algorithm

Source: Internet
Author: User

Main learning from: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

Problem Description: Palindrome string is the right and left symmetric string, such as: "ABBA", and the longest palindrome substring is the longest string length palindrome substring, such as "Abbaca" the longest palindrome substring "ABBA".

Conventional solution: It is obvious that nested loops can be used to "brute force" settlement of the answer, its time complexity is O (n^2), and the Manacher algorithm is a more time-saving algorithm, its temporal complexity is O (n).

Main ideas:

First fill the string with # (or something else), and declare an array to record the radius of each character in the string as the center, back to the text character, so that it looks like this:

str = # a # b # # a # C # a # C # a # B # C # a # C #

arr = 0 1 0 3 0 1 0 3 0 7 0 3 0 1 0 1 0 1 0 3 0 1 0

Thus we can see that the effect of adding # is to make the string whether it is an odd length or even length, after processing can be counted in each character as the center of the palindrome radius, and even-length palindrome cannot count this, such as: "ABBA"

After the calculation of arr, it is easy to get the maximum palindrome substring length of 7, which is "Bacacab".

So the key to this algorithm is to compute arr, and by observing we find that the values in arr are actually symmetrical:

For example above, to t[11] as the symmetry Center (palindrome radius of 9), required t[13] in the palindrome radius, according to its relative to t[11] symmetry t[9] to calculate, obviously arr[13]=arr[9]. The Arr[i]=arr[i '] can be computed on this example.

But this method is not always applicable, such as:

When i=15, because Arr[i ']>r-i, so arr[15] is not necessarily equal to the symmetric arr[7], as in this example arr[15]=5. So we get the following rules:

If arr[i ']≤r–i,
Then arr[i]←arr[i ']
else p[i]≥r–i.

Know how to calculate the palindrome radius, how to determine the Palindrome center (that is, C in the figure)?

In the same way as I+arr[i]>r, replace C with I, R to I+arr[i]

Direct Sticker Code:

stringPreprocess (stringstr) {    //$ is the beginning of the identification, and the end of the string is identified as    stringencoded ="$"; intstrlen =str.length (); //The purpose of the addition # is to make the text string of even digits more easily distinguishable, such as: "1221", in the calculation of palindrome length array, bad calculation, but $1#2#2#1\0 of the palindrome length (RADIUS) array is [0,0,0,1,3,1,0,0,0]     for(inti =0; i < strlen; i++) {encoded+="#"+ STR.SUBSTR (i,1); } encoded+="#"; returnencoded;}stringLongestpalindrome (stringstr) {    stringtemp =preprocess (str); //the center of a palindrome string    intPalindrome_center =0; //right boundary of palindrome string    intRight_border =0; intstrlen =temp.length (); //used to store the radius length of the back character string centered on that point, note that this is the radius length, because the string is processed and contains the #    int* Plength_array =New int[strlen]; //I relative to center of symmetry Place    intMirror_i =0; //The loop starts at 1 because the identifier "$" begins     for(inti =1; i < strlen; i++)    {        //I relative to the center of the palindrome symmetrical placeMirror_i =2* Palindrome_center-i; if(mirror_i>0&& Right_border > I)//I in the back text clause string        {            //if the range does not exceed the right boundary, the palindrome number is symmetrical            if(Right_border-i >Plength_array[mirror_i]) plength_array[i]=Plength_array[mirror_i]; ElsePlength_array[i]= Right_border-i;//* Take a minimum value first        }        ElsePlength_array[i]=0; //the case of first taking a minimum value above *         while(Temp[i +1+ Plength_array[i]] = = Temp[i-1-Plength_array[i]]) Plength_array[i]++; //if the length of the text string is longer than the existing right boundary, the new center and right boundary are established        if(i + plength_array[i] >Right_border) {Palindrome_center=i; Right_border= i +Plength_array[i]; }    }    //find the largest element in Plength_array    intMaxLen =0; //The center of the longest palindrome    intMaxlen_center =0;  for(intj =0; J < Strlen; J + +)    {        if(plength_array[j]>maxlen) {MaxLen=Plength_array[j]; Maxlen_center=J; }    }    Delete[] plength_array; returnStr.substr ((maxlen_center-1-maxlen)/2, MaxLen);}

Find the 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.