Codeforces 17E:P alisection (palindrome automatic machine)

Source: Internet
Author: User

Transmission Door

Test instructions
To the string s, find the logarithm of all the palindrome strings that intersect in S.

Exercises
Previously wrote an article Manacher \text{manacher}, but today I found that palindrome automatic machine so good, manacher \text{manacher} I should not touch again. (unless the card space, time is actually no different).

This code MLE MLE, but the machine ran run should be right, by the way to write a summary of the palindrome automatic machine.

For the fail fail on a palindrome is the longest palindrome suffix of this palindrome, son son indicates that the palindrome can be used to extend the palindrome string to both sides.

If all fail fail is reversed, the palindrome machine becomes Dag Dag, it is more obvious that a palindrome string if it is another palindrome string, then on the Dag dag this palindrome can reach another palindrome string.

Palindrome Automatic machine adopts incremental construction.
A position after joining the palindrome machine, the last point to the current letter end of the longest palindrome suffix, constantly upward jump fail fail chain can get all the current letter end of the palindrome suffix.
You can then count the occurrences of all palindrome strings (that is, the number of occurrences of the palindrome that points to it for all of its fail fail and the number of occurrences of it itself).

Then the problem is relatively simple, the number of illegal statistics can be. For each location to maintain the number of palindrome suffixes ending with the current letter, then crossdress the same statistic, then the statistical contribution can be.

#include <bits/stdc++.h> using namespace std;
const int mod=51123987;
const int N=2E6+50;
Char Ch[n];
int n,len[n],fail[n],dep[n],cnt[n],cnt2[n],son[n][26],tot,lst;
    inline int getfail (int pos,int nn) {while (Ch[nn-len[pos]-1]!=ch[nn]) Pos=fail[pos];
return POS;
    } int main () {len[0]=0; len[1]=-1; fail[0]=1; dep[0]=0; dep[1]=0; Tot=1;
    Lst=1;
    scanf ("%d%s", &n,ch+1);
        for (int i=1;i<=n;i++) {int v=ch[i]-' a ';
        int Now=getfail (lst,i);
            if (!son[now][v]) {len[++tot]=len[now]+2;
            Fail[tot]=son[getfail (Fail[now],i)][v];
            Son[now][v]=tot;
        dep[tot]=dep[fail[tot]]+1; } Lst=son[now][v];
    CNT[I]=DEP[LST];
    } reverse (ch+1,ch+n+1); 
    memset (son,0,sizeof (son));
    memset (dep,0,sizeof (DEP));
    memset (fail,0,sizeof (fail));
    memset (len,0,sizeof (len)); len[0]=0; Len[1]=-1;
    Fail[0]=1; dep[0]=0; dep[1]=0; Tot=1;
    Lst=1; for (int i=1;i<=n;i++) {int v=ch[i]-' a '; (Cnt[i]+=CNT[I-1])%=mod;
        int Now=getfail (lst,i);
            if (!son[now][v]) {len[++tot]=len[now]+2;
            Fail[tot]=son[getfail (Fail[now],i)][v];
            Son[now][v]=tot;
        dep[tot]=dep[fail[tot]]+1; } Lst=son[now][v];
    CNT2[N-I+1]=DEP[LST];
    } const int inv=25561994; int rs=0;
    int all=cnt[n];
    All= (Long Long) all*all%mod-all+mod)%mod;
    All= (long Long) all*inv%mod;
    for (int i=1;i<=n;i++) {rs= (rs+ (Long Long) cnt[i]*cnt2[i+1]%mod)%mod;
} cout<< (All-rs+mod)%mod; }

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.