O (n) palindrome substring (manacher) algorithm

Source: Internet
Author: User

O (n) palindrome substring (manacher) algorithm

Data source network See: http://www.felix021.com/blog/read.php?2040

Problem Description:

Enter a string to find the largest palindrome substring in the list. The meaning of a substring is: a string fragment that appears consecutively in the original string. The meaning of a palindrome is: look and look backwards, like Abba and Yyxyy.

Analytical:

The O (n) palindrome string (manacher) algorithm is introduced here

The basic point of the algorithm: first, in a very ingenious way, all possible odd/even length palindrome strings are converted to odd lengths: Insert a special symbol on either side of each character. For example, Abba becomes #a #b#b#a#, ABA becomes #a #b#a#. To further reduce the complexity of the encoding, you can add another special character at the beginning of the string, so that you do not have to deal with cross-border issues specifically, such as $ #a #b#a#.

The following is an example of string 12212321, which, after the previous step, became s[] = "$ #1 #2#2#1#2#3#2#1#";

Then use an array of p[i] to record the length (including S[i]) of the longest palindrome string centered on the character S[i], such as the corresponding relationship of 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 total length of the palindrome string in the original P.S)

The following calculation P[i], the algorithm adds two auxiliary variable ID and MX, where the ID represents the largest palindrome substring center position, MX is id+p[id], that is, the maximum palindrome substring boundary.

The key point of this algorithm is here: if mx > I, then p[i] >= MIN (p[2 * id-i], mx-i).

The specific code is as follows:

if (mx > i) {      p[i] = (p[2*id-i] < (mx-i) p[2*id-i]: (MX- i));} else1;}      

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 are symmetric, 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], see.

When P[j] > mx-i, the palindrome string centered on s[j] is not completely contained in a palindrome string centered on S[id], but based on symmetry, it is known that the part surrounded by the two green boxes is the same, that is, the palindrome string centered on the s[i], and its right will expand to the position of MX at least. , which means p[i] >= mx-i. As to whether the post-MX part is symmetrical, it is only one match.

For the case of MX <= i, p[i] can not be made more assumptions, only p[i] = 1, and then to match the

The following gives the original text, further explaining the reason for the linearity of the algorithm

#include <iostream>#include<cmath>#include<algorithm>#include<string.h>#include<stdio.h>#defineMAXN 110055using namespacestd;Charma[maxn*2];intmp[maxn*2];intl;intManacher (CharS[],intLen) {    intres =0; L=0; Ma[l++] ='$'; Ma[l++] ='#';  for(intI=0; i<len; i++) {ma[l++] =S[i]; Ma[l++] ='#'; } Ma[l]=0; intMX =0, id =0;  for(intI=0; i<l; i++) {Mp[i]= mx >i min (mp[2*id-i],mx-i):1;  while(Ma[i+mp[i] [= Ma[i-mp[i]]) Mp[i] + +; if(i + mp[i] >mx) {mx= i +Mp[i]; ID=i; }    }}CharS[MAXN];intMain () {//#ifndef Online_judge//freopen ("In.txt", "R", stdin);//#endif//Online_judge     while(~SCANF ("%s", s)) {        intLen =strlen (s);        Manacher (S,len); intAns =0;  for(intI=0; i<l; i++) {ans=Max (ans,mp[i]); } printf ("%d\n", ans-1); }    return 0;}

O (n) 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.