Manarcher the longest palindrome substring of a string "record"

Source: Internet
Author: User
Tags mx record record mx

Disclaimer: Only the implementation process is written here. Want to learn manacher can see here the implementation of the process, the algorithm involves some of the principles recommended a blog.

It feels very thin.


Introduction: Given a string s, let you find the longest palindrome substring length.



The algorithm roughly implements the process:


one: In order to exclude the effect of the palindrome string length odd or even. A new string of STR is first inserted between every two characters, and the characters that are not present in the original string (# are used here). set P[i] to the maximum radius of the back-text character string centered on the str[i] character. the length of the palindrome string centered on Str[i] in the new string is p[i]-1.


Two: string from front to back p[] array. (Don't ask why before and after)


Three: Enumerate all P[i] values and update the maximum value.



Manacher essence--p[] array. First we have found the previous P[j] value (0<=j<=i-1) when we are seeking p[i]

Preparation of P[i]:

Use MX record max{k+p[K]} (0<=k<=i-1)-the rightmost position in front of which all palindrome strings can be overwritten .

record MX with ID k-- in front of all palindrome strings can be overwritten to the far right when the maximum value is taken The location of the palindrome string centered on the character Str[id] .


(1) According to the previous p[] p[i]

One, mx > i--with Str[id] as the center of the palindrome string to the character str[i] covered.

In this case we can get: p[i]= min (p[2*id-i], mx-i). (why?) Please see my recommended blog)


Two, MX <= i--with Str[id] as the center of the palindrome string at the end of the character str[i] or not overwrite to the character Str[i].

In this case p[i] = 1, because Palindrome has only its own. (why?) Please see my recommended blog)


(2) Of course, the above treatment is not enough, because the processing of the resulting p[i] is not necessarily the optimal palindrome radius we want.

Why Please see my recommended blog)

The follow-up process is well understood.

while (str[i + p[i]] = = str[I-p[i]]) p[i]++;//extends to the left and right until it cannot extend.


(3) Each time the p[i] is found, the ID to be used for the calculation of p[i+1] can be obtained first.




Code implementation:

#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 110100using namespace Std;char s [maxn];//Original string int str[maxn*2];//new string note array size int p[maxn*2];void manacher (char *t) {    int len = strlen (T);    int l = 0;    str[l++] = ' @ ';//Prevent cross-border    str[l++] = ' # ';    for (int i = 0; i < len; i++)    {        str[l++] = t[i];        str[l++] = ' # ';    }    Str[l] = 0;    int mx = 0, id = 0;    int ans = 0;    for (int i = 0; i < l; i++)    {        if (mx > i)//2*id-i is the symmetric point about ID            p[i] = min (p[2*id-i], mx-i);        else            p[i] = 1;        Left and right extension        while (str[i+p[i]] = = Str[i-p[i]]) p[i]++;        if (i + p[i] > mx)//Find calculation p[i+1] Use the ID        {            mx = i + p[i];            id = i;        }        ans = max (p[i]-1, ans);    }    printf ("%d\n", ans);} int main () {while    (scanf ("%s", s)! = EOF)    {        manacher (s);//the longest palindrome substring length of the string s    }    return 0;}





I have the data structure is very slag, the wrong place to welcome correct. (⊙o⊙)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Manarcher the longest palindrome substring of a string "record"

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.