Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define rep(i, a, b) for (int i = (a), i##_end = (b); i < i##_end; ++i)
- #define per(i, a, b) for (int i = (a), i##_end = (b); i >= i##_end; --i)
- using namespace std;
- typedef unsigned uint;
- const int N = 9e4, SIGMA = 26;
- struct Node {
- Node* word[SIGMA], *link;
- int len;
- uint sum[SIGMA];
- } node[2*N], * root = node;
- int sz = 1;
- void add(int c){
- static Node* last = root;
- Node* now = node + sz++, * p = last;
- last = now;
- now->len = p->len + 1;
- for(;p && !p->word[c]; p = p->link) p->word[c] = now;
- if (!p) {
- now->link = root;
- return;
- }
- Node* q = p->word[c];
- if (q->len == p->len + 1) {
- now->link = q;
- } else {
- Node* clone = node + sz++;
- *clone = *q;
- clone->len = p->len + 1;
- for(;p && p->word[c] == q; p = p->link) p->word[c] = clone;
- now->link = q->link = clone;
- }
- }
- bool vis[2*N];
- uint dfs(Node* no){
- bool& v = vis[no-node];
- if (v) return no->sum[SIGMA-1];
- v = true;
- uint s = 1;
- rep (i, 0, SIGMA) {
- Node* v = no->word[i];
- if (v) s += dfs(v);
- no->sum[i] = s;
- }
- return s;
- }
- void query(uint k){
- Node* x = root;
- ++k;
- while (k > 1) {
- int c = 0;
- while (x->sum[c] < k) ++c;
- putchar('a' + c);
- k -= c ? x->sum[c-1] : 1;
- x = x->word[c];
- }
- putchar('\n');
- }
- char s[N+1];
- int main(){
- scanf("%s", s);
- int n = 0;
- while (s[n]) add(s[n++] - 'a');
- dfs(root);
- int q;
- scanf("%d", &q);
- while (q--) {
- uint k;
- scanf("%u", &k);
- query(k);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment