SUBLEX - Lexicographical Substring Search suffix-array-8 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.
#include<stdio.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<bitset> #include<string> #include<stack> #include<cmath> #include<queue> #include<set> #include<map> usingnamespacestd; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define fin(name) freopen(name,"r",stdin) #define fout(name) freopen(name,"w",stdout) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedeflonglong LL; constdouble PI = acos(-1.0); constint N = 90010; structTrie { int nxt[26], pre, len; LL cnt; voidinit() { for (int i = 0; i < 26; ++i) nxt[i] = -1; pre = -1; cnt = 1; } } L[N << 1]; int sz, last; char s[N]; int cnt[N], x[N << 1]; int ans[N];
inlinevoidinit() { sz = last = 0; L[sz++].init(); //L[0].cnt = 0; CLR(cnt, 0); CLR(ans, 0); } inlineintnewnode() { L[sz].init(); return sz++; } voidins(int c) { int u = newnode(); L[u].len = L[last].len + 1; int t = last; while (t != -1 && L[t].nxt[c] == -1) { L[t].nxt[c] = u; t = L[t].pre; } if (t == -1) L[u].pre = 0; else { int v = L[t].nxt[c]; if (L[t].len + 1 == L[v].len) L[u].pre = v; else { int np = newnode(); L[np] = L[v]; L[np].len = L[t].len + 1; L[np].pre = L[v].pre; L[u].pre = L[v].pre = np; while (t != -1 && L[t].nxt[c] == v) { L[t].nxt[c] = np; t = L[t].pre; } } } last = u; } intmain(void) { int i, j, q; LL k; while (~scanf("%s", s)) { init(); int len = strlen(s); for (i = 0; i < len; ++i) ins(s[i] - 'a'); for (i = 0; i < sz; ++i) ++cnt[L[i].len]; for (i = 1; i <= len; ++i) cnt[i] += cnt[i - 1]; for (i = sz - 1; i >= 0; --i) x[--cnt[L[i].len]] = i; for (i = sz - 1; i >= 0; --i) { int u = x[i]; for (j = 0; j < 26; ++j) { int v = L[u].nxt[j]; if (~v) L[u].cnt += L[v].cnt; } } scanf("%d", &q); while (q--) { scanf("%lld", &k); int u = 0; while (k) { for (i = 0; i < 26; ++i) { int v = L[u].nxt[i]; if (v == -1) continue; if (k <= L[v].cnt) { --k; putchar(i + 'a'); u = v; break; } else k -= L[v].cnt; } } puts(""); } } return0; }