Finding the Manacher algorithm of the longest palindrome string

Source: Internet
Author: User

Given a string S, find the longest palindromic substring in s. The maximum length of S is assume, and there exists one unique longest palindromic substring.

1, the violent solution.

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, and the time complexity of the algorithm is O (n^3) , which is obviously unsatisfactory.

2. The midpoint of the enumeration.

A slightly optimized algorithm is to enumerate the midpoint of 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), However, when the n ratio is still not satisfactory, the Manacher algorithm can find the longest string of strings in linear time complexity, which achieves the theoretical lower bound.

3. 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

Len[i] is calculated first from left to right, and Len[j] (0<=j<i) is calculated when len[i] is calculated. 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, such as:

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, indicates that the palindrome string centered on I may extend beyond p, and that the portion greater than P is not 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].

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].

1 classSolution {2  Public:3     stringLongestpalindrome (strings) {4        intpos =0;5        intBestl =1;6        intn =s.size ();7        stringNewString ="#";8         for(inti =0; i<n;i++){9newstring+= S.substr (i,1);Tennewstring+="#"; One        } Avector<int> Lens (2*n+1,1); -        //find the longest palindrome string -         for(intI=0; I<lens.size (); i++){ the            if(i< (pos+Bestl)) { -Lens[i] = min (bestl+pos-i,lens[2*pos-i]); -}Else{ -Lens[i] =1; +            } -             for(intk = Lens[i];(i-k>=0) && (K+i<lens.size ()); k++){ +                if(newstring[i-k]==newstring[i+K]) { Alens[i]++; at}Else{ -                     Break; -                } -            } -            //Update -            if(lens[i]>=Bestl) { inBestl =Lens[i]; -pos =i; to            } +        } -        //take it out. the        stringresult=""; *        intBegin = ((POS-BESTL) +1)/2; $Result + = S.substr (begin,bestl-1);Panax Notoginseng         -         the        returnresult; +     } A};

Finding the Manacher algorithm of the longest palindrome 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.