1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include <bits/stdc++.h> #define MAX_N 500000 using namespace std; char s[MAX_N+5]; int l, T, K, tot[MAX_N+5]; int rt, sz, lst, val[(MAX_N<<1)+5], sum[(MAX_N<<1)+5]; struct node {int ch[26], len, par;} SAM[(MAX_N<<1)+5]; int newnode(int _len) {SAM[++sz].len = _len; return sz;} void init() {sz = 0, rt = lst = newnode(0);} void insert(int c) { int p = lst, np = newnode(SAM[p].len+1); lst = np, val[np] = 1; for (; p && !SAM[p].ch[c]; p = SAM[p].par) SAM[p].ch[c] = np; if (!p) {SAM[np].par = rt; return;} int q = SAM[p].ch[c]; if (SAM[q].len == SAM[p].len+1) {SAM[np].par = q; return;} int nq = newnode(SAM[p].len+1); memcpy(SAM[nq].ch, SAM[q].ch, sizeof SAM[q].ch); SAM[nq].par = SAM[q].par, SAM[q].par = SAM[np].par = nq; for (; p && SAM[p].ch[c] == q; p = SAM[p].par) SAM[p].ch[c] = nq; } int prt(int c, int k) { if (k <= val[c]) return 1; k -= val[c]; for (int i = 0; i < 26 && k > 0; k -= sum[SAM[c].ch[i++]]) if (SAM[c].ch[i] && k <= sum[SAM[c].ch[i]]) putchar('a'+i), prt(SAM[c].ch[i], k); return 1; } int main() { int que[(MAX_N<<1)+5]; init(); scanf("%s%d%d", s, &T, &K), l = (int)strlen(s); for (int i = 0; i < l; i++) insert(s[i]-'a'); for (int i = 1; i <= sz; i++) tot[SAM[i].len]++; for (int i = 1; i <= l; i++) tot[i] += tot[i-1]; for (int i = sz; i; i--) que[tot[SAM[i].len]--] = i; for (int i = sz; i; i--) if (!T) val[que[i]] = 1; else val[SAM[que[i]].par] += val[que[i]]; val[1] = 0; for (int i = 1; i <= sz; i++) sum[i] = val[i]; for (int i = sz; i; i--) for (int j = 0; j < 26; j++) sum[que[i]] += sum[SAM[que[i]].ch[j]]; return (K > sum[rt] ? puts("-1") : prt(rt, K)), 0; }
|