[Notes] manacher learning algorithm

A. About manacher

  manacher palindromic substring algorithm for solving a string length of the radius.

  It can be linearly solve for the radius of the longest palindromic sequence each of the character string to itself as the center.

  And this palindrome string with each character as the center of this substring is a palindrome string.

  

  The time complexity of the algorithm is O (n).

II. Palindromic parity sub-problems caused by the length of the string.

  When a palindromic sequence length is odd, it must have a central palindrome, the palindrome on either side of the central portion are symmetrical to each other.

  So when a palindrome string length is even the time palindrome center ...... it does not, then how to do it?

  We consider artificial construct a palindrome center.

  The method of construction is to insert a same original character string must be not in the middle of each of the two characters in the string, so we can find:

  When the length of the substring of the original string palindromic is odd, the center of which is the palindrome original character string.

  When palindrome substring of the original string length is even, it is the center of the palindrome our newly inserted characters.

  All right. The perfect solution to this problem.

Three .manacher algorithm flow.

  We once again confirm, manacher algorithm for solving the string for each character to itself as the center of the radius of the longest palindrome string.

  We each character string to itself as the center of the radius of the longest palindromic sequence entered an array called p [].

  The time complexity of O (n) To complete this matter, we must solve the p array by recursive way.

  

  1. palindromic symmetry string

    We mentioned above palindrome string symmetry.

    Suppose now we have a string: s [11]: abababababa

    We can see clearly that he is a palindromic sequence string, the string palindrome is a palindrome central s [5] (starting with index 0).

    Observation s [2] as the center of a palindromic sequence, and is not s [8] is equal to the length of a palindromic sequence is exactly equal to the center?

  2. The two important parameters: MaxR, Mid.

    So we can p [2] Push to p [8], but these are preconditions, i.e., p [5] ≥4.

    Because the central premise palindrome is a palindrome large string into two sub-strings palindromic symmetry center point, they can expand the radius of the longest palindromic sequence must be the same.

    So we have to maintain the right point and center Mid MaxR large palindrome string to ensure the transfer.

    But this shift is not necessarily so that p [] is the longest palindrome radius, so we expanded violence on the line.

  Overall complexity O (n).

 

III. Code

  Note that the left and right side of the string in the process of expanding outward to be conferred on the different symbols might otherwise TLE.

  Luo Gu [template] manacher algorithm:

#include<bits/stdc++.h>
using namespace std;
long long n,ans,p[22000002];
char c[11000001],s[22000002];
int main()
{
    scanf("%s",c+1);
    n=strlen(c+1);
    for(int i=1;i<=n;i++)
    {
        s[i*2]=c[i];s[i*2-1]='!';
    }
    s[0]='+';
    s[2*n+1]='!';
    s[2*n+2]='-';
    long long MaxR=0,Mid=0;
    for(int i=1;i<=n*2;i++)
    {
        if(i<MaxR) p[i]=min(MaxR-i,p[Mid*2-i]);
        else p[i]=0;
        while(s[i-p[i]-1]==s[i+p[i]+1]) p[i]++;
        if(i+p[i]>MaxR)
        {
            MaxR=i+p[i];
            Mid=i;
        }
    }
    for(int i=1;i<=n*2;i++)
    {
        ans=max(ans,p[i]);
    }
    printf("%lld",ans);
}
View Code

 

 

    

Guess you like

Origin www.cnblogs.com/Rakan-LoveJ/p/11404833.html
Recommended