-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2108.cpp
75 lines (70 loc) · 1.4 KB
/
2108.cpp
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
int sz, last, len[N], link[N], nxt[N][26];
ll cnt[N];
void sa_init() {
len[0] = 0;
link[0] = -1;
sz = 1; last = 0;
}
void sa_extend(int c) {
int cur = sz++, p;
len[cur] = len[last] + 1;
for (p = last; p != -1 && !nxt[p][c]; p = link[p]) {
nxt[p][c] = cur;
}
if (p == -1) {
link[cur] = 0;
} else {
int q = nxt[p][c];
if (len[p] + 1 == len[q]) {
link[cur] = q;
} else {
int clone = sz++;
len[clone] = len[p] + 1;
link[clone] = link[q];
memcpy(nxt[clone], nxt[q], sizeof(nxt[q]));
for (; p != -1 && nxt[p][c] == q; p = link[p]) {
nxt[p][c] = clone;
}
link[cur] = link[q] = clone;
}
}
last = cur;
}
void dfs(int u) {
cnt[u] = 1;
for (int i = 0; i < 26; ++i) {
if (!nxt[u][i]) continue;
int v = nxt[u][i];
if (!cnt[v]) dfs(v);
cnt[u] += cnt[v];
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
string s; cin >> s;
sa_init();
for (char c: s) {
sa_extend(c - 'a');
}
dfs(0);
ll k; cin >> k;
int cur = 0;
while (k > 0) {
for (int i = 0; i < 26; ++i) {
if (!nxt[cur][i]) continue;
int v = nxt[cur][i];
if (cnt[v] < k) {
k -= cnt[v];
} else {
cout << (char)(i + 'a');
--k; cur = v;
break;
}
}
}
return 0;
}