[BZOJ3238] [Ahoi2013] Difference (suffix array + monotonic stack | | Suffix automaton + Tree dp)

Source: Internet
Author: User
Title Description

Portal

The problem is actually very interesting.
First, according to the description of the topic the answer should be the combined length of all suffixes minus 22 LCP
First figure out the sum
To find the SA and height, with two times the monotone stack can be found at a point of height as the minimum value of the longest interval
Can be found at this point as the demarcation point, the left and right sides of the interval 22 combination minimum must be the height of the current point, that is, the length of the LCP
And then we can calculate the answer.

And then the problem is the suffix automaton.
Because it is LCP, in turn, the suffix automaton is established, and the two suffix LCS is actually the LCA of two states on the PA tree
So the tree-shaped dp,f (i) indicates how many pairs with the first point of the LCA, G (i) represents the subtrees of f (i), so that the code can be DP

#include <iostream> #include <cstring> #include <cstdio> using namespace std;
#define LL Long Long #define N 500005 Char s[n];
int n,m,top;
int *x,*y,x[n],y[n],c[n],sa[n],height[n],rank[n];
int stack[n],l[n],r[n],last[n];

LL ans;
    void Build_sa () {m=200;
    X=x,y=y;
    for (int i=0;i<m;++i) c[i]=0;
    for (int i=0;i<n;++i) ++c[x[i]=s[i]];
    for (int i=1;i<m;++i) c[i]+=c[i-1];

    for (int i=n-1;i>=0;--i) sa[--c[x[i]]]=i;
        for (int k=1;k<=n;k<<=1) {int p=0;
        for (int i=n-k;i<n;++i) y[p++]=i;

        for (int i=0;i<n;++i) if (sa[i]>=k) y[p++]=sa[i]-k;
        for (int i=0;i<m;++i) c[i]=0;
        for (int i=0;i<n;++i) ++c[x[y[i]];
        for (int i=0;i<m;++i) c[i]+=c[i-1];

        for (int i=n-1;i>=0;--i) sa[--c[x[y[i]]]]=y[i];
        Swap (x, y);
        p=1,x[sa[0]]=0; for (int i=1;i<n;++i) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&& (sa[i-1]+k<n?y[sa[i-1]+k]:-1) = = (sa[i]+k<n?y[sa[i]+k]:-1)? p-1:p++;
        if (p>n) break;
    M=p;
    }} void Build_height () {for (int i=0;i<n;++i) rank[sa[i]]=i;
    int k=0;height[0]=0;
        for (int i=0;i<n;++i) {if (!rank[i]) continue;
        if (k)--k;
        int j=sa[rank[i]-1];
        while (I+k<n&&j+k<n&&s[i+k]==s[j+k]) ++k;
    Height[rank[i]]=k;
    }} int main () {scanf ("%s", s); N=strlen (s);
    Build_sa ();
    Build_height ();

    for (int i=1;i<=n;++i) ans+= (LL) i* ((ll) n-1);
    Top=0;
        for (int. i=0;i<n;++i) {while (Top&&height[i]<=height[stack[top]])--top;
        if (!top) l[i]=0;
        else l[i]=stack[top]+1;
    Stack[++top]=i;
    } top=0;
        for (int. i=n-1;i>=0;--i) {while (Top&&height[i]
Related Article

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.