Bzoj 3998 string Theory TJOI2015

Source: Internet
Author: User
problem

Bzoj Solution

Build a suffix automaton, and then we can use the idea of a binary-like search tree. First, the topological order is discharged, then the number of substrings represented by this node is processed, and then the enumeration of the small of the k is in it.
We might as well make g[u] represent the number of suffixes that can be represented from the node U, so you can do a topological motion.
We can easily get the equation:
G[U]=SZ[U]+∑U−>VG[V] g [u] = S Z [u] +∑u−> v G [v] g[u]=sz[u]+\sum_{u->v}g[v]
Note that when you calculate a repeating substring, you sz[f[i]]+=sz[i]. Code

#include <cstring> #include <cstdio> using namespace std;
const int maxn=1000010;
int t,k,n,lst,cnt,now=1,len,pre;
int A[MAXN],B[MAXN],C[MAXN],F[MAXN],L[MAXN],SZ[MAXN],CH[MAXN][26],G[MAXN];
Char s[maxn>>1];
    void Insert (int c) {int p=lst,np=++cnt;
    Lst=np;l[np]=l[p]+1;sz[np]=1;
    For (;p &&!ch[p][c];p =f[p]) CH[P][C]=NP;
    if (!p) f[np]=1;
        else {int q=ch[p][c];
        if (l[q]==l[p]+1) f[np]=q;
            else {int nq=++cnt;l[nq]=l[p]+1;if (!t) sz[nq]=1;
            Memmove (ch[nq],ch[q],sizeof (ch[q));
            F[NQ]=F[Q];F[Q]=F[NP]=NQ;
        for (; ch[p][c]==q;p=f[p]) ch[p][c]=nq;
    }}} int main () {#ifndef Online_judge freopen ("In.txt", "R", stdin);
    #endif scanf ("%s%d%d", s+1,&t,&k);
    N=strlen (s+1); lst=cnt=1;
    for (int i=1;i<=n;i++) Insert (s[i]-' a ');
    for (int i=1;i<=cnt;i++) c[l[i]]++;
    for (int i=1;i<=cnt;i++) c[i]+=c[i-1]; for (int i=1;i<=cnt;i++) a[c[L[i]]--]=i;
    for (int i=cnt,j;t&&i;i--) {j=a[i];sz[f[j]]+=sz[j];}
    sz[1]=sz[0]=0;
        for (int i=cnt,j;i;i--) {j=a[i];g[j]=sz[j];
    for (int r=0;r<26;r++) g[j]+=g[ch[j][r]];
    } if (g[1]<k) {puts ("1"); return 0;}
        while (K>sz[now]) {K-=sz[now];
            for (int i=0;i<26;i++) {if (K>g[ch[now][i]]) k-=g[ch[now][i];
        Else{putchar (i+ ' a '); now=ch[now][i];break;}
    }} putchar (' \ n ');
return 0; }

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.