日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SPOJ 7258 SUBLEX 后缀自动机

發布時間:2023/12/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SPOJ 7258 SUBLEX 后缀自动机 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

求第k大子串。

按拓撲序處理出一個點往后有多少條路徑到終態。
答案就很明顯了。

#include <cstring> #include <cstdio> #define FOR(i,j,k) for(i=j;i<=k;++i) const int rt = 1, N = 500005; int last = 1, cnt = 1, len = 0; int trans[N][26], fa[N], ma[N], v[N], b[N], l[N], bucket[N]; char str[N]; void add(char c) {int np = ++cnt, p = last, q, nq; last = np; ma[np] = ++len;while (p && !trans[p][c]) trans[p][c] = np, p = fa[p];if (!p) fa[np] = rt;else {q = trans[p][c];if (ma[q] == ma[p] + 1) fa[np] = q;else {nq = ++cnt; memcpy(trans[nq], trans[q], sizeof trans[q]);ma[nq] = ma[p] + 1; fa[nq] = fa[q]; fa[q] = fa[np] = nq;while (p && trans[p][c] == q) trans[p][c] = nq, p = fa[p];}} }void sort(int *x, int *y, int n, int m) {int i;FOR(i,1,n) ++bucket[x[i]];FOR(i,1,m) bucket[i] += bucket[i - 1];for(i=n;i;--i) y[bucket[x[i]]--] = i; }int main() {int i, j, n, q, o, k, p;scanf("%s", str + 1);n = strlen(str + 1);FOR(i,1,n) add(str[i] - 'a');sort(ma, b, cnt, n);FOR(i,1,cnt) v[i] = 1;for (i = cnt; i; --i) FOR(j,0,25)if (trans[b[i]][j]) v[b[i]] += v[trans[b[i]][j]];for (scanf("%d", &o); o--; putchar('\n')) {scanf("%d", &k); p = rt;while (k) FOR(i,0,25) if (trans[p][i]) {q = trans[p][i];if (k > v[q]) k -= v[q];else {putchar(i + 'a');p = trans[p][i];--k;break;}}}return 0; }

SUBLEX - Lexicographical Substring Search

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

總結

以上是生活随笔為你收集整理的SPOJ 7258 SUBLEX 后缀自动机的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。