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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include <cstdio> #include <string> #include <vector> #include <iostream> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...rest> void read(Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } typedef long long LL; const int N = 1e6 + 10; struct Node { int p, len, nxt[26]; } tr[N]; int hd = 1, tl, q[N]; int d[N], last = 1, cnt = 1; LL h[N], f[N]; vector <int> g[N]; void insert(int c) { int p = last, np = ++cnt; tr[np].len = tr[p].len + 1; h[np] = 1; for (; p && !tr[p].nxt[c]; p = tr[p].p) tr[p].nxt[c] = np; last = np; if (!p) return void(tr[np].p = 1); int q = tr[p].nxt[c]; if (tr[q].len == tr[p].len + 1) return void(tr[np].p = q); int nq = ++cnt; tr[nq] = tr[q], tr[nq].len = tr[p].len + 1; tr[q].p = tr[np].p = nq; for (; p && tr[p].nxt[c] == q; p = tr[p].p) tr[p].nxt[c] = nq; } int main () { string str; cin >> str; for (char c : str) insert(c - 'a'); for (int i = 1; i <= cnt; ++i) for (int j = 0; j < 26; ++j) if (tr[i].nxt[j]) g[i].push_back(j), ++d[tr[i].nxt[j]]; q[++tl] = 1; while (hd <= tl) { int t = q[hd++]; f[t] = 1; for (int i : g[t]) if (!--d[tr[t].nxt[i]]) q[++tl] = tr[t].nxt[i]; } int T, k; read(T, k); if (T) for (int i = hd; i; --i) h[tr[q[i]].p] += h[q[i]]; else for (int i = 1; i <= cnt; ++i) h[i] = 1; h[1] = 0; for (int i = 1; i <= cnt; ++i) f[i] = h[i]; for (int i = hd; i; --i) for (int j : g[q[i]]) f[q[i]] += f[tr[q[i]].nxt[j]]; for (int p = 1; k <= f[p]; ) { if ((k -= h[p]) <= 0) return 0; for (int i : g[p]) if (f[tr[p].nxt[i]] >= k) { putchar(i + 'a'); p = tr[p].nxt[i]; break; } else k -= f[tr[p].nxt[i]]; } puts("-1"); return 0; }
|