1. 程式人生 > >Bzoj3998: [TJOI2015]弦論

Bzoj3998: [TJOI2015]弦論

space urn fill 後綴 math size || bzoj3998 mem

題面

傳送門

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]弦論