码迷,mamicode.com
首页 > 其他好文 > 详细

Bzoj3998: [TJOI2015]弦论

时间:2018-04-21 16:08:11      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:xtend   bre   scan   printf   NPU   mem   lse   else   AC   

题面

传送门

Sol

\(sam\)
求一个串的不重复的第\(k\)小子串很好办
如果可以相同
那么要算上每个点(前缀)的后缀的个数
那么就是这个\(endpos(right)\)集合的子串的出现次数

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
# define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
using namespace std;
typedef long long ll;

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

const int maxn(1e6 + 5);

int trans[26][maxn], fa[maxn], len[maxn], tot = 1, last = 1;
int n, f[maxn], t[maxn], id[maxn], g[maxn];
char s[maxn];

IL void Extend(RG int c){
    RG int p = last, np = ++tot; last = tot;
    len[np] = len[p] + 1, g[np] = 1;
    while(p && !trans[c][p]) trans[c][p] = np, p = fa[p];
    if(!p) fa[np] = 1;
    else{
        RG int q = trans[c][p];
        if(len[q] == len[p] + 1) fa[np] = q;
        else{
            RG int nq = ++tot;
            len[nq] = len[p] + 1, fa[nq] = fa[q];
            for(RG int i = 0; i < 26; ++i) trans[i][nq] = trans[i][q];
            fa[q] = fa[np] = nq;
            while(p && trans[c][p] == q) trans[c][p] = nq, p = fa[p];
        }
    }
}

IL void Calc(RG int k){
    RG int nw = 1;
    if(k <= f[nw]){
        while(k > 0){
            for(RG int i = 0; i < 26; ++i){
                RG int p = trans[i][nw];
                if(p){
                    if(k > 0 && k <= f[p]){
                        k -= g[p], putchar(i + 'a'), nw = p;
                        break;
                    }
                    else k -= f[p];
                }
            }
        }
    }
    else printf("-1");
    puts("");
}

int main(RG int argc, RG char* argv[]){
    scanf(" %s", s), n = strlen(s);
    for(RG int i = 0; i < n; ++i) Extend(s[i] - 'a');
    for(RG int i = 1; i <= tot; ++i) ++t[len[i]];
    for(RG int i = 1; i <= tot; ++i) t[i] += t[i - 1];
    for(RG int i = 1; i <= tot; ++i) id[t[len[i]]--] = i;
    if(Input()){
        for(RG int i = tot; i; --i)
            if(fa[id[i]]) g[fa[id[i]]] += g[id[i]];
        f[1] = 0;
        for(RG int i = tot; i; --i){
            f[id[i]] = g[id[i]];
            for(RG int j = 0; j < 26; ++j) f[id[i]] += f[trans[j][id[i]]];
        }
    }
    else{
        for(RG int i = 1; i <= tot; ++i) f[i] = 1, g[i] = 1;
        for(RG int i = tot; i; --i)
            for(RG int j = 0; j < 26; ++j) f[id[i]] += f[trans[j][id[i]]];
    }
    Calc(Input());
    return 0;
}

Bzoj3998: [TJOI2015]弦论

标签:xtend   bre   scan   printf   NPU   mem   lse   else   AC   

原文地址:https://www.cnblogs.com/cjoieryl/p/8901644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!