[BZOJ3998] String Theory (TJOI2015)-Suffix Automata

Test address:String Theory
practice:Suffix automata is required for this question.
Let me start with a digression: Today is a special day, that is, Ben Konjac succeeded in AC100 questions at BZOJ! Congratulations, congrats...
Okay, well, having said that, this question requires two things: K Large substring and K Substrings that are different in nature. Just look at this data range, you know O ( n log n ) The suffix array must be very clumsy (of course if you will O ( n ) The construction is as if I didn't say...), and according to the nature of the suffix automata, each different path from the starting point corresponds to a substring of different nature, then we build a suffix automaton for the string. Then we discuss the situation:
Seeking K Substrings that are different in nature. We only need to DFS once to find the number of substrings starting from each point, and then follow this on the suffix automata.
Seeking K Big substring. This is more complicated than the above, because it requires the number of occurrences of each substring. It is observed that each prefix of a string makes a contribution of 1 to all its suffixes. According to the definition of suffix link in suffix automata, in fact, a prefix will end on the path from its root to the suffix link. All substrings make a contribution of 1, so if we know which points are prefix nodes, we can go through DFS to find the number of occurrences of each substring. Which points are prefix nodes? In fact, as long as they are not "cloned" points that appear after a certain point when building a suffix automaton, they are all prefix nodes. Then after we find the number of occurrences of each substring, we can do it according to the first case.
The time complexity of all the steps above is O ( n ) Yes, it is very complicated.
The following is my code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,tot=0,last,pre[1000010],ch[1000010][26],len[1000010];
int T,k,first[1000010]={0},tote=0;
int q[1000010],h,t;
ll cnt[1000010],f[1000010]={0};
char s[500010];
bool vis[1000010]={0},cln[1000010]={0};
struct edge
{
    int v,next;
}e[1000010];

void init()
{
    scanf("%s",s);
    scanf("%d%d",&T,&k);
}

void extend(char c)
{
    int p,q,np,nq;
    np=++tot;
    len[np]=len[last]+1;
    p=last;
    while(p&&!ch[p][c-'a']) ch[p][c-'a']=np,p=pre[p];
    if (!p) pre[np]=1;
    else
    {
        q=ch[p][c-'a'];
        if (len[p]+1==len[q]) pre[np]=q;
        else
        {
            nq=++tot;
            cln[nq]=1;
            len[nq]=len[p]+1;
            pre[nq]=pre[q];
            for(int i=0;i<26;i++)
                ch[nq][i]=ch[q][i];
            while(p&&ch[p][c-'a']==q) ch[p][c-'a']=nq,p=pre[p];
            pre[q]=pre[np]=nq;
        }
    }
    last=np;
}

void insert(int a,int b)
{
    e[++tote].v=b;
    e[tote].next=first[a];
    first[a]=tote;
}

void build()
{
    n=strlen(s);
    last=++tot;
    pre[last]=len[last]=0;
    for(int i=0;i<26;i++)
        ch[last][i]=0;
    for(int i=0;i<n;i++)
        extend(s[i]);
    for(int i=2;i<=tot;i++)
        insert(pre[i],i);
}

void dfs1(int v)
{
    vis[v]=1;
    cnt[v]=cln[v]?0:1;
    for(int i=first[v];i;i=e[i].next)
    {
        if (!vis[e[i].v]) dfs1(e[i].v);
        cnt[v]+=cnt[e[i].v];
    }
    if (T==0) cnt[v]=1;
    if (v==1) cnt[v]=0;
}

void dfs2(int v)
{
    vis[v]=1;
    f[v]=cnt[v];
    for(int i=0;i<26;i++)
        if (ch[v][i])
        {
            if (!vis[ch[v][i]]) dfs2(ch[v][i]);
            f[v]+=f[ch[v][i]];
        }
}

void findans(int v,ll k)
{
    if (k>f[v]) {printf("-1");return;}
    k-=cnt[v];
    if (k<=0) return;
    for(int i=0;i<26;i++)
        if (ch[v][i])
        {
            if (f[ch[v][i]]<k) k-=f[ch[v][i]];
            else
            {
                printf("%c",i+'a');
                findans(ch[v][i],k);
                break;
            }
        }
}

int main()
{
    init();
    build();
    dfs1(1);
    memset(vis,0,sizeof(vis));
    dfs2(1);
    findans(1,k);

    return 0;
}

Intelligent Recommendation

[BZOJ3998]-[TJOI2015] String Theory-Suffix Automata

last UPD at 2018.3.14 corrected the previous hand sliding Speak ahead Tortured to death by this question... The first time I wrote SAM, I didn’t understand enough, so I was trapped by various de...

[BZOJ3998][TJOI2015] String Theory (Suffix Automata + Topological Sorting + Greedy)

First construct the suffix automaton of the original string. Then through topological sorting on the suffix automata, the number of strings that can be reached by each edge is preprocessed (if the pos...

[String theory] bzoj3998 suffix automata

Description What is for a given string of length N, find its first string K kid. Input The first line is a character string consisting of only S lowercase letters The second line of two integers and T...

BZOJ3998] [string theory (suffix automata)

BZOJ3998] [string theory (suffix automata) Face questions BZOJ answer This question should be very simple construct\(SAM\)Rear is determined for each point in the future but also to build a few string...

[bzoj3998][TJOI2015] string theory - suffix automaton

The meaning of the topic: Given a string, find its kth substring. Idea: Template question for the suffix automaton. After considering the suffix automaton, we will find out how many times each state a...

More Recommendation

[Bzoj3998] [TJOI2015] string theory suffix automaton + dp

Title Description What is for a given string of length N, find its first string K kid. Entry The first line is a character string consisting of only S lowercase letters The second line of two integers...

BZOJ3998 TJOI2015 string theory suffix automaton [] [greedy]

Description What is for a given string of length N, find its first string K kid. Input The first line is a character string consisting of only S lowercase letters T and the second row of two integers ...

[BZOJ3998][TJOI2015] String Theory-Suffix Tree

String Theory Description For a string with a given length of N, what is the Kth smallest substring? Input The first line is a string S consisting of only lowercase English letters The second line is ...

[TJOI2015] string theory (suffix automata)

3998: [TJOI2015] String Theory Description What is for a given string of length N, find its first string K kid. Input The first line is a character string consisting of only S lowercase letters T and ...

[TJOI2015] - string theory (suffix automata)

Portal First saw this question two sets of forced Lexicographically smallest first sub-string is definitely on greedy automaton And if we take the first step in the smallest transfer So after the firs...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top