Palindrome tree summary

First, the palindrome is a palindrome tree automata.

Indeed \ (\ texttt Tham} {\) , in \ (the CSP \) simulation game which test \ (the PAM \)(Then the audience would I cut)

Issues into

Seeking a lowercase letter 'a' - 'z' constitute essentially different number of strings palindromic substring.

problem solved

violence

First consider substrings each enumerated, and then determines it is not a palindromic sequence \ (the Hash \) into \ (Map \) or \ (Hash-table \) inside. Complexity \ (\ Theta (n ^ 3 ) \)

Optimization of violence

Think judgment is not palindromic sequence by reverse twice (the Hash \) \ obtained. Complexity \ (\ Theta (^ n-2) \) .

manacher

The process is not considered manacher all palindromic sequence can be determined, pretreatment \ (the Hash \) values followed by \ (Map \) or \ (Hash-table \) can be done \ (\ Theta (n) \ ) or \ (\ Theta (nlogn) \ )

PAM

This is what we want to say algorithm - palindrome automaton.

form

It is easy to know, palindrome string two, one length is odd a length is even
while walking down the palindrome trees, we are certainly not the only one in the back to add a character
is obviously a character before and after each add
so we can easily come to that, if the string may become another palindrome string
then its length must add an even number
so palindrome trees, in order to distinguish between the two palindromic strings
so the tree is equivalent to a palindrome forest
two trees, a representative length of palindromic sequence is an odd number, the other representative of a palindromic sequence length is an even number

structure

Construction, then consider the incremental construction.
Join a position \ (r \) , then you can find the longest palindrome suffix.

Code

int tot=1,last;
char s[N];
struct node{
    int son[26],len,ff;
}t[N<<1];
void extend(int c,int n){
    int p=last;
    while(s[n]!=s[n-t[p].len-1])p=t[p].ff;
    if(!t[p].son[c]){
        int np=++tot,k=t[p].ff;t[np].len=t[p].len+2;
        while(s[n]!=s[n-t[k].len-1])k=t[k].ff;
        t[np].ff=t[k].son[c];
        t[p].son[c]=np;
    }
    last=t[p].son[c];
}

Guess you like

Origin www.cnblogs.com/fexuile/p/11627718.html