Find the longest palindrome string, O (n) the optimal algorithm Manacher

Source: Internet
Author: User

The first step of the algorithm is to add a # to the left and right of each character, so what effect does it have?

For example, after ABA initialization is #a#b#a#, the string length is 7 odd.

For example, after 1221 initialization is #1#2#2#1#, the string length is 9 odd.

Why we want to convert it to an odd number, because the algorithm to retrieve the length of the string, the need to have a central node, and then to the left and right to search, so you need to convert palindrome string to odd length.

Then we need to assign a value of str[0] to a character, can be assigned to $, not to do so, but we can start processing the string from 1, we know that the C + + array is starting from 0.

The second step of the algorithm enters the interior of the manacher algorithm. In fact, this algorithm uses the symmetry of palindrome string, makes an optimization, so that we do not have to take a one of the calculation. For example, S is a palindrome string, we calculate the right part of a node when we find it compound palindrome characteristics, then we know that the left half of s must also exist a same node, in the range of s of the elements of this node are the same, However, it is possible that the palindrome represented by this node will exceed the length of the s string, which is certain to occur. But you can save a part of your job.


The algorithm has an array of type int, such as we define as int p[200]; The meaning of p[i] is to represent the length of the I-node around the string of characters.

The algorithm also has two auxiliary variables Centerid and maxeage, which represent the value of the center node and its p[i], which is the length of the palindrome string respectively.

I feel the algorithm core is in p[i] = Min (p[2*centerid-i],p[centerid]+centerid-i), it will determine whether I is in a palindrome string, If you continue to judge the P-value and Maxeage of the palindrome-string symmetric point J with I as the node, the least one is taken, because the feasibility of optimization can be ensured only in palindrome. If I is not in a palindrome, then P is assigned a value of 1, and the left and right of the search to find the P-value.


Here is the source code, there are some comments, you can look at.

<span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm>using namespace std; #define Min (x, y) ((x) < (y)? ( x):(y)) Char inputstring[100];//holds the input's original string char teststring[200];//holds the string int after initialization p[200];//    The teststring string length int len;//string length void Manacher () {int I, which is centered on each element in the store.    int centerid,maxeage;    The Centerid represents the center//maxeage of the maximum return string and represents the length of the maximum return string, that is, the 2maxeage-1 is the length of the strings.    maxeage = 0;    Centerid = 0; for (i = 1; i < Len; i++) {if (Maxeage > I)//If I is within the range of the longest back character string {P[i] = Min (p[2*centerid-i],p[centerid]+c    ENTERID-I);    j = 2*centerid-i is the symmetric point of I//is actually compared to the I-centered palindrome string is within the maximum palindrome string, compared to the distance between P[j] and I to maxeage.    } else {P[i] = 1;//itself is counted, so the minimum length is 1} for (; Teststring[i+p[i]]==teststring[i-p[i]]; p[i]++);    if (P[i] + i > Maxeage)//If you find a longer palindrome string, replace it in time.    {maxeage = p[i] + i;    Centerid = i; }}//Because the middle node of the string is also maxeage, it should -1}void start () {int i;len = strlen (Inputstrin) when extracting the left and right boundsg); teststring[0] = ' $ '; teststring[1] = ' # '; for (i = 0; i < len; i++) {teststring[i*2+2] = inputstring[i];teststring[i*2+3 ] = '#';} Len = len*2 + 2;//cout<<teststring<<endl;//can verify that the initialized string is correct//cout<<len<<endl;// You can output the length of the initialization string}int main () {int answer = 0,i,jiedian = 0;memset (p,0,sizeof (P)); memset (Teststring,0,sizeof (teststring)); Cin.getline (inputstring,80); start (); Manacher (); for (i=0;i<len;i++) {if (P[i] > answer) {answer            = P[i];         Jiedian = i;    }}//cout<<jiedian<<endl;//can locate the center point of the longest-returning character string//cout<<p[jiedian]<<endl;    cout<< "Longest palindrome string:"; for (i = Jiedian-p[jiedian] + 1; I <= Jiedian + P[jiedian]-1; i++) {if (teststring[i] = = ' # ') c        Ontinue;    cout<<teststring[i]; }cout<<endl<< "string length:" <<answer-1<<endl;return 0;} </span>




Find the longest palindrome string, O (n) the optimal algorithm Manacher

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.