#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using LL = long long;
const int kN = 5e5 + 1;
struct SAM {
struct E {
int n[26], ml, pr, c, v;
LL d;
vector<int> e;
} e[kN << 1];
int c, l;
SAM() { e[0].pr = -1; }
void I(char ch) {
ch -= 'a';
int x = ++c, p = l;
e[x].ml = e[l].ml + 1, e[x].c = 1;
for (; ~p && !e[p].n[ch]; e[p].n[ch] = x, p = e[p].pr) {
}
if (~p) {
int q = e[p].n[ch];
if (e[q].ml == e[p].ml + 1) {
e[x].pr = q;
} else {
int _q = ++c;
copy(e[q].n, e[q].n + 26, e[_q].n);
e[_q].ml = e[p].ml + 1, e[_q].pr = e[q].pr, e[q].pr = _q;
for (; ~p && e[p].n[ch] == q; e[p].n[ch] = _q, p = e[p].pr) {
}
e[x].pr = _q;
}
}
l = x;
}
int D(int x) {
for (int i : e[x].e) {
e[x].c += D(i);
}
return e[x].c;
}
void Sc() {
for (int i = 1; i <= c; ++i) {
e[e[i].pr].e.push_back(i);
}
D(0);
}
LL Sd(int x, int ty) {
if (e[x].d) {
return e[x].d;
}
if (x) {
e[x].d = e[x].v = (ty ? e[x].c : 1);
}
for (int i = 0; i < 26; ++i) {
int y;
if (y = e[x].n[i]) {
e[x].d += Sd(y, ty);
}
}
return e[x].d;
}
string Kth(LL k, int ty) {
if (k >= e[0].d) {
return "-1";
}
int x = 0;
string s = "";
for (; k;) {
for (int i = 0; i < 26; ++i) {
int y;
if (y = e[x].n[i]) {
LL v = e[y].d;
if (v >= k) {
s += i + 'a', x = y, k -= e[y].v;
break;
} else {
k -= v;
}
}
}
}
return s;
}
} t;
string s;
int ty, k;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> s >> ty >> k;
for (char c : s) {
t.I(c);
}
t.Sc(), t.Sd(0, ty);
cout << t.Kth(k, ty);
return 0;
}
我的这份代码可以通过此题,但是可以被下面这组数据 hack,原因是当 k==e[0].d 时是存在第 k 小子串的,但我的程序误以为不存在。
input:
aaa
0 3
answer:
aaa
my_output:
-1