--manacher algorithm for finding the longest palindrome substring

Source: Internet
Author: User

Palindrome string including odd-length and even-length, the general demand for the time to discuss the situation, the algorithm has done a simple processing to unify the parity situation. The basic idea of the algorithm is this, the original string each character in the middle with a string of characters not appearing in the character (uniform parity), with an array p[i] record the str[I] as the middle character of the palindrome string to the right to match the length. Let's look at an example.

Original string: W a A B w s W f D

New String (str): # w # a # a # B # w # s # w # F # d #

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

P Array: 1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1

By the nature of the P-array, the length of the palindrome string with Str[i] as the middle character in the new string is p[i]-1 ( can control p[11] This position, p[i]-1 itself represents the symmetry radius, but actually remove # after, P[i]-1 is the palindrome string length ), in order to # For the middle character is the length is even, the non-# is the middle character is the length is odd, then how to find p[] array?

From left to right calculation (0~str.length), that is, calculate p[i], p[0.....i-1] has been calculated, and with a variable MX records the current detected palindrome string the right maximum position max{k+p[K]} (K=0.....I-1), The k at which the maximum value is admitted by ID.


The above is used by a lot of people, it should be noted that the two graphs represent the current point i<mx two cases:

1) The current point I about the ID of the symmetric point J, J-centered palindrome string of the left edge is not less than id-p[id], according to the symmetry of palindrome string, which means I the Palindrome string length is the same as J, so there is p[i] = p[j] = P[2*id-i];

2) If the left edge of the palindrome string centered on J is less than Id-p[id], you can only ensure that p[i]>=mx-i, as for the value of p[i], also need to detect the position behind the MX to determine it.

So we have the following key code, understand this part, the whole algorithm is good to understand.

if (mx >= MIN (p[2*id-i], mx-i);

The complete code is as follows:

1#include <iostream>2#include <string>3#include <stdlib.h>4 using namespacestd;5 6 Charcarray[ +];7 intp[ +];8 9 intManacher (intlength)Ten { One     intMX =0; A     intID =0; -     intMaxLength =0; -      the      for(intI=0; i<length; ++i) -     { -         if(mx>i) -         { +P[i] = min (p[2*id-i], mx-i); -         } +         Else A         { atP[i] =1; -         } -  -          while((i-p[i]+1) >=0&& (i+p[i]-1) <length && carray[i-p[i]+1]==carray[i+p[i]-1] ) -         { -P[i] = P[i] +1; in         } -  top[i]--; +  -         if(i+p[i]-1>mx) the         { *MX = i+p[i]-1; $ID =i;Panax Notoginseng         } -  the         if(MaxLength < p[i]-1) +         { AMaxLength = p[i]-1; the         } +     } -  $  $     returnmaxLength; - } -  the  - Wuyi  the intMain () - { Wu     //string input = "WAABWSWFD"; -     stringinput ="Wawbbbwasaw"; About     intK =0; $      for(intI=0; I<input.size (); ++i) -     { -carray[k++] ='#'; -carray[k++] =input.at (i); A     } +carray[k++] ='#'; the     intAns =Manacher (k); -cout << ans <<Endl; $}

--manacher algorithm for finding 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.