Sorting algorithm to follow suit Manacher

It was the last day a fairy room talking about, \ (gu \) took so long to organize \ (w \) , the basic idea has been talked about godAll forgetFortunately, the god wrote \ (Blog \) , the blowout original Bo On \ (Manacher \) algorithm , as well as the original Bo fairy \ (YCH \) !
And then blowing a wave of \ (YCH \) :

too huge a!

\ (Manacher \) a \ (O (n) \) seeking palindromic character substring algorithm. (Remember to ask then confused fairy \ (ych \) asha diaoQuestion: substring is a continuous thing? Obviously this palindrome is contiguous substrings;

\(Solution:\)

For a bunch of strings for each character which we have to maintain a \ (R [i] \) represents the longest palindrome radius of the string, but this time there was \ (bug \) :

\ (Ykyyky \)

\ (Ykykyky \)

For a substring before, palindromic substring is even, then an odd palindromic palindromic substring substring. This time we express how they palindrome radius difference? \ (3 \) and \ (3.5 \) ? ✘ this time we can add between each string ' \ (\ #' \)

\ (\ # And \ #k \ #and \ #and \ #k \ #and \ # \)

\ (\ # And \ #k \ #and \ #k \ #and \ #k \ #and \ # \)

Thus palindromic so that their radius to uniquely determine;

Processing of: \ (R & lt [I] \) represents the radius of the longest palindromic, when we find the location of each \ (R & lt [I] \) , when added \ ( '\ #' \) after \ ( R [i] _ {max} -1 \) is the longest we require palindromic sequence length (Sensibility For example Li Jie

How to deal with?

Asked \ (R [i] \)

Before setting \ (i-1 \) the maximum value of the right end of the palindromic sequence number is \ (R & lt \) , the right to obtain the maximum number of points is \ (MID \) . Obviously \ (r = mid + R [ mid] \)

\(\mathfrak{A}.\)\(i\leq r\)

Calculation \ (I \) on \ (MID \) symmetric point \ (J = I-MID * 2 \) ,

\ (\ mathfrak {A}. \) \ (jR [J]> MID-R & lt [MID] \) , i.e. \ (I \) range of the palindromic sequence contained in the point of symmetry \ (MID \) corresponds to point palindrome sequence range, \ (I \) palindromic sequence and \ (J \) a palindromic sequence must be symmetrically distributed (since \ (i, j \) on \ (MID \) symmetrically and \ (MID \) within a palindromic radius), the \ (R [i] = R [j] \)

\ (\ mathfrak {B}. \) \ (jR [J] \ MID-R & lt Leq [MID] \) , this time about the \ (i, j \) on \ (MID \) symmetrically and \ ( mid \) must be symmetrical in the palindrome radius, but outside the radius of palindromic symmetry if we do not know, so we use the most simple and crude way: violence expansion;

\(\mathfrak{B}.i>r\)

So violent expansion √

After completion of the above three each attempt to update \ (R & lt, MID \) :

if(r<i+R[i]) {
    r=i+R[i]-1;
    mid=i;
}

Then complexityWill not permit, (I must have too dishes.

\(Code:\)

Code amount is not large, pay attention to the head and tail string must insert a \ ( '\ #' \)

#include<bits/stdc++.h>

using namespace std;

char s[22000703];
int R[22000703],len;

void read() {
    char ch=getchar();
    s[0]='~';s[++len]='#';
    while(ch>'z'||ch<'a') ch=getchar();
    while(ch>='a'&&ch<='z') s[++len]=ch,s[++len]='#',ch=getchar();
}

int main () {
    read();
    int r=0,mid=0,ans=0;
    for(int i=1;i<=len;i++) {
        if(i<=r) R[i]=min(R[2*mid-i],r-i+1);
        while(s[i-R[i]]==s[i+R[i]]&&s[i-R[i]]!='~') ++R[i];
        if(r<i+R[i]) {
            r=i+R[i]-1;
            mid=i;
        }
        ans=max(ans,R[i]);
    }
    printf("%d",ans-1);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zhuier-xquan/p/12092722.html