Manacher algorithm----the longest palindrome substring

Source: Internet
Author: User

Title Description

Given a string, find the length of its longest palindrome substring.

Analysis and Solution

The easiest way to do this is to enumerate all the substrings and determine whether they are palindrome. This idea seems to be correct at first, but it does a lot of useless work, if a long substring contains another short substring, then the palindrome judgment of the substring is not necessary. Also, the odd and even lengths are considered separately.

The Manacher algorithm solves these problems and results are obtained in O (n) time complexity. Let's take a look at the manacher algorithm.

First, to deal with parity problems, insert a special symbol on either side of each character so that all odd or even lengths are converted to odd lengths. For example, ABC becomes #a#b#c#.

Also, to avoid handling cross-border problems, insert another special character at the beginning of the string. For example, $ #a #b#c#.

In the case of string 12212321, insert the # and $ two special symbols into s[] = "$ #1 #2#2#1#2#3#2#1#", and then use an array p[i] to record the length of the longest palindrome string centered on the character S[i] to the left or right (including s[i]).

such as the correspondence between S and P:

    • S # 1 # 2 # 2 # 1 # 2 # 3 # 2 # 1 #
    • P 1 2 1 2 5 2 1 4 1 2 1 6 1 2 1 2 1

As you can see, p[i]-1 is exactly the length of the longest palindrome in the original string, which is 5.

Next you need to calculate the P[i],manacher algorithm requires two helper variable ID and MX, where the ID represents the maximum palindrome substring center location, MX is id+p[id], that is, the boundary of the largest palindrome substring. You can get a conclusion:

    • If MX > I, then p[i] >= Min (p[2 * id-i], mx-i)

Let's look at how this conclusion is obtained.

Make j = 2*id-i, that is, J is the symmetric point of I about ID.

When Mx-i > P[j], the palindrome string centered on S[j] is contained in a palindrome string centered on S[id], because I and J symmetry, the palindrome string centered on s[i] is necessarily contained in a palindrome string centered on S[id], so there must be p[i] = p[j];

When P[j] >= mx-i, the palindrome string centered on s[j] is not necessarily completely contained in a palindrome string centered on S[id], but based on symmetry, the two green boxes are surrounded by the same part, that is, a palindrome string centered on S[i], It will expand to the right at least to the MX position, i.e. P[i] >= mx-i. As to whether the post-MX parts are symmetrical, then match exactly.

In addition, for the case of MX <= I, because you cannot make more assumptions about p[i], you can only let p[i] = 1, and then go to match.

From this, we can draw a conclusion. Through the above algorithm, there is code:

1 intManacher (Char*s) {2     //new A string, insert ' $ ', ' # '3     //For example s = "aba" NewS = "$ #a #b#a#"4     intLen =2* STRLEN (s) +3;5     Char*news =New Char[Len];6     intj =1;7news[0] ='$';8      for(inti =0; S[i]! =' /'; ++i) {9News[j++] ='#';TenNews[j++] =S[i]; One     } ANews[j++] ='#'; -NEWS[J] =' /'; -  the     //begin -     int*p =New int[Len-1]; -     intMX =0, id =0; -Memset (P,0,sizeof(P)); +     intMaxLen =0; -      for(inti =1; News[i]! =' /'; ++i) { +         if(MX >i) { AP[i] = min (p[2* Id-i], MX-i); at         } -         Else -P[i] =1; -          while(News[i + p[i]] = = News[i-P[i]]) -++P[i]; -         if(i + p[i] >MX) { inMX = p[i] +i; -ID =i; to         } +         if(P[i] >maxlen) -MaxLen = P[i]-1; the     } *  $     returnMaxLen;Panax Notoginseng}

Manacher algorithm----the longest palindrome substring

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.