SPOJ SUBLEX - Lexicographical Substring Search

SUBLEX - Lexicographical Substring Search

no tags

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:aaa223

Output:
aaaaa

解题:SAM。我们可以算路径,来计算它后面有多少个字串,因为路径个数就代表了不同子串的个数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 200010;
 4 struct node {
 5     int son[26],f,len;
 6     void init() {
 7         f = -1;
 8         len = 0;
 9         memset(son,-1,sizeof son);
10     }
11 };
12 struct SAM {
13     node e[maxn<<1];
14     int tot,last;
15     void init() {
16         tot = last = 0;
17         e[tot++].init();
18     }
19     int newnode(int len = 0) {
20         e[tot].init();
21         e[tot].len = len;
22         return tot++;
23     }
24     void extend(int c) {
25         int p = last,np = newnode(e[p].len + 1);
26         while(p != -1 && e[p].son[c] == -1) {
27             e[p].son[c] = np;
28             p = e[p].f;
29         }
30         if(p == -1) e[np].f = 0;
31         else {
32             int q = e[p].son[c];
33             if(e[p].len + 1 == e[q].len) e[np].f = q;
34             else {
35                 int nq = newnode();
36                 e[nq] = e[q];
37                 e[nq].len = e[p].len + 1;
38                 e[q].f = e[np].f = nq;
39                 while(p != -1 && e[p].son[c] == q) {
40                     e[p].son[c] = nq;
41                     p = e[p].f;
42                 }
43             }
44         }
45         last = np;
46     }
47 } sam;
48 char str[maxn];
49 int c[maxn],sa[maxn],dp[maxn],ans[maxn];
50 node *e = sam.e;
51 void solve(){
52     int Q,k;
53     scanf("%d",&Q);
54     while(Q--){
55         scanf("%d",&k);
56         int tot = 0,p = 0;
57         while(k){
58            for(int i = 0; i < 26; ++i){
59                 if(dp[e[p].son[i]] < k) k -= dp[e[p].son[i]];
60                 else {
61                     ans[tot++] = i;
62                     p = e[p].son[i];
63                     --k;
64                     break;
65                 }
66            }
67         }
68         for(int i = 0; i < tot; ++i)
69             putchar(ans[i] + ‘a‘);
70         putchar(‘\n‘);
71     }
72 }
73 int main() {
74     sam.init();
75     scanf("%s",str);
76     int len = strlen(str);
77     for(int i = c[len] = 0; str[i]; ++i) {
78         sam.extend(str[i] - ‘a‘);
79         c[i] = 0;
80     }
81     for(int i = 1; i < sam.tot; ++i) {
82         c[e[i].len]++;
83         dp[i] = 1;
84     }
85     for(int i = 1; i <= len; ++i) c[i] += c[i-1];
86     for(int i = sam.tot-1; i > 0; --i) sa[c[e[i].len]--] = i;
87     for(int i = sam.tot-1; i > 0; --i){
88         int v = sa[i];
89         for(int j = 0; j < 26; ++j)
90             if(e[v].son[j] != -1) dp[v] += dp[e[v].son[j]];
91     }
92     solve();
93     return 0;
94 }

时间: 2024-11-03 15:11:13

SPOJ SUBLEX - Lexicographical Substring Search的相关文章

SPOJ SUBLEX Lexicographical Substring Search - 后缀数组

题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] - height[i]$. 求出$height$数组后,求一求本质不同的子串个数的前缀和,可以对每个询问二分. 这里可以直接离线,$O(n + m)$扫一扫就好了. Code 1 /** 2 * SPOJ 3 * Problem#SUBLEX 4 * Accepted 5 * Time: 30ms

SPOJ 7258 Lexicographical Substring Search(后缀自动机)

[题目链接] http://www.spoj.com/problems/SUBLEX/ [题目大意] 给出一个字符串,求其字典序排名第k的子串 [题解] 求出sam上每个节点被经过的次数,然后采用权值线段树上查找第k大数类似的方法, 每次确定查找范围,进入相应的子树,同时输出路径上的点即可. [代码] #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

SPOJ SUBLEX 7258. Lexicographical Substring Search

看起来像是普通的SAM+dfs...但SPOJ太慢了......倒腾了一个晚上不是WA 就是RE ..... 最后换SA写了...... Lexicographical Substring Search Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description Little Daniel loves to play wi

Lexicographical Substring Search SPOJ - SUBLEX (后缀自动机)

Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串,求出这个字符串上字典序第 \(k\) 小的子串. 思路 先对给出的字符串构建后缀自动机,因为后缀自动机上从根节点走到任意节点都是原串的一个子串,所以我们可以 \(dfs\) 求出节点 \(i\) 往后存在多少个子串. 对于查询第 \(k\) 小的子串时,在用一个 \(dfs\) 来求,对于当前节点

Lexicographical Substring Search SPOJ - SUBLEX (后缀数组)

Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串,求出这个字符串上字典序第 \(k\) 小的子串. 思路 对于给出的字符串,求出后缀数组,根据后缀数组的 \(height\) 数组,我们可以很容易得到一个字符的总子串个数是 \(\sum_{i=1}^{n} (n-sa[i]+1+height[i])\),利用这个式子,就可以求出第 \(k\) 小

【SPOJ】7258. Lexicographical Substring Search(后缀自动机)

http://www.spoj.com/problems/SUBLEX/ 后缀自动机系列完成QAQ...撒花..明天or今晚写个小结? 首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一个子串,而且不重复.(原因似乎是逆序后缀树? 所以我们在自动机上预处理每一个状态的子串数目,然后从小到大枚举字符. 子串数目可以这样预处理出:s[x]=sum{s[y]}+1, y是x出发的下一个点,意思就是说,以x开头的子串有那么多个(即将孩子的所有子串前边都加上x),然后x单独算一个子串. 然后

【spoj7528】 Lexicographical Substring Search

http://www.spoj.com/problems/SUBLEX/ (题目链接) 题意 给出一个字符串,询问其中字典序第K小的子串. Solution 后缀自动机例题. 构出后缀自动机以后,对每个节点预处理出从这个节点可以到达多少个不同的子串.然后就是类似于在平衡树上查找一样沿着SAM一路查找下去一路更新答案了. 代码借鉴的DaD3Zz大神,感觉好优美. 细节 后缀自动机的节点数是2N的. 代码 // spoj7528 #include<algorithm> #include<io

[SPOJ7258]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 substri

D - 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 o