-
Notifications
You must be signed in to change notification settings - Fork 3
/
substring-order-II.cpp
54 lines (47 loc) · 1.36 KB
/
substring-order-II.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
// ALLOWS REPETITIONS
string kth_smallest_substring(const string& s, ll k) {
/* uses /strings/suffix-automaton.cpp
add 'cnt' and 'nmb' to state struct with (0, -1);
=> for new states 'not cloned': cnt = 1
create 'order' vector to iterate by length in decreasing
vector<pair<int, int>>: {len, id}
=> for each new state add to 'order' vector
to do not allow repetitions:
=> remove: kth+=s.size, sort(order) for(l, p : order)
=> add: st[clone].cnt = 1 (sa_extend)
*/
string ans;
k += s.size();
SuffixAutomaton sa(s);
sort(all(order), greater<pair<int, int>>());
// count and mark how many times a substring of a state occurs
for (auto& [l, p] : order) sa.st[sa.st[p].link].cnt += sa.st[p].cnt;
auto dfs = [&](auto&& self, int u) {
if (sa.st[u].nmb != -1) return;
sa.st[u].nmb = sa.st[u].cnt;
for (int i = 0; i < 26; ++i) {
if (sa.st[u].next[i]) {
self(self, sa.st[u].next[i]);
sa.st[u].cnt += sa.st[sa.st[u].next[i]].cnt;
}
}
};
dfs(dfs, 0);
int u = 0;
while (sa.st[u].nmb < k) {
k -= sa.st[u].nmb;
for (int i = 0; i < 26; i++) {
if (sa.st[u].next[i]) {
int v = sa.st[u].next[i];
if (sa.st[v].cnt < k)
k -= sa.st[v].cnt;
else {
ans.push_back(i + 'a');
u = v;
break;
}
}
}
}
return ans;
}