Little Daniel loves to play with strings! He always finds different ways to have fun with strings! Knowing that, his friend Kinan decided to test his skills so he gave him a string S and asked him Q questions of the form:
If all distinct substrings of string S were sorted lexicographically, which one will be the K-th smallest?
After knowing the huge number of questions Kinan will ask, Daniel figured out that he can't do this alone. Daniel, of course, knows your exceptional programming skills, so he asked you to write him a program which given S will answer Kinan's questions.
Example:
S = "aaa" (without quotes)
substrings of S are "a" , "a" , "a" , "aa" , "aa" , "aaa". The sorted list of substrings will be:
"a", "aa", "aaa".
Input
In the first line there is Kinan's string S (with length no more than 90000 characters). It contains only small letters of English alphabet. The second line contains a single integer Q (Q <= 500) , the number of questions Daniel will be asked. In the next Q lines a single integer K is given (0 < K < 2^31).
Output
Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.
Example
Input: aaa 2 2 3 Output: aa aaa
题意
给你一个字符串和Q次询问,每次问你字典序第k小的子串是哪个。
题解
建立后缀自动机,维护每个节点往后可以到多少个节点, 查询的时候沿着边贪心往后走。
dp[i] = + 1.
代码
#include <bits/stdc++.h>
#ifdef LOCAL
#define debug(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define debug(x) 1;
#endif
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lowbit(x) x&-x
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 1e6 + 5;
const int maxn = 2e6 + 100;
struct SAM
{
// ch 转移函数, pre父亲, len最长长度, tag前缀标记, in入度, endnum endpos的个数 o顺序
int ch[maxn][26],pre[maxn],len[maxn],tag[maxn],in[maxn],endnum[maxn];
int last, tot;
void init(){
last = tot = 0;
memset(ch[0], -1, sizeof ch[0]);
pre[0] = -1; len[0] = in[0] = 0;
}
// 加多个串时为了保证不跨串算
void reset() {last = 0;}
int extend(int c, int ind){
int p = last, np = ++tot;
len[np] = len[p] + 1; tag[np] = ind;
memset(ch[np], -1, sizeof ch[np]);
in[np] = 0;
while(~p && ch[p][c] == -1) ch[p][c] = np, p = pre[p];
if(p == -1) {
pre[np] = 0;
in[0]++;
}
else{
int q = ch[p][c];
if(len[q] != len[p] + 1){
int nq = ++tot;
memcpy(ch[nq], ch[q], sizeof ch[q]);
in[nq] = tag[nq] = 0;
len[nq] = len[p] + 1;
pre[nq] = pre[q];
pre[q] = pre[np] = nq;
in[nq] += 2;
while(~p && ch[p][c] == q) ch[p][c] = nq, p = pre[p];
}
else {
pre[np] = q;
in[q]++;
}
}
last = np;
return len[np] - len[pre[np]];
}
// 按照pre树计数排序 who为第i个位置的编号
int who[maxn], a[maxn];
void Sort() {
for(int i = 1; i <= tot; i++) a[i] = 0;
for(int i = 1; i <= tot; i++) a[len[i]]++;
for(int i = 1; i <= tot; i++) a[i] += a[i - 1];
for(int i = 1; i <= tot; i++) who[a[len[i]]--] = i;
}
void top_sort() {
queue<int> q;
for(int i = 1; i <= tot; i++) {
if(!in[i]) q.push(i);
endnum[i] = 0;
}
while(!q.empty()) {
int u = q.front();
q.pop();
if(tag[u]) endnum[u]++;
if(u) endnum[pre[u]] += endnum[u];
if(u) if(!--in[pre[u]]) q.push(pre[u]);
}
}
ll d[maxn];
void presolve() {
Sort();
for(int i = tot; i >= 1; i--) {
ll sum = 0;
int p = who[i];
for(int j = 0; j < 26; j++)
if(~ch[p][j]) sum += d[ch[p][j]];
d[p] = sum + 1;
}
}
void solve(int k) {
int now = 0;
vector<char> ans;
while(k) {
for(int i = 0; i < 26; i++) {
int np = ch[now][i];
if(np == -1) continue;
if(d[np] >= k) {
now = np;
ans.pb(i + 'a');
k--;
break;
} else k -= d[np];
}
}
for(char i : ans) putchar(i);
puts("");
}
}sam;
char s[MAXN];
int main() {
#ifdef LOCAL
freopen ("input.txt", "r", stdin);
#endif
scanf("%s", s + 1);
sam.init();
int n = strlen(s + 1);
for(int i = 1; i <= n; i++) sam.extend(s[i] - 'a', i);
sam.presolve();
int q;
scanf("%d", &q);
while(q--) {
int k;
scanf("%d", &k);
sam.solve(k);
}
return 0;
}