Dicription

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

  1. Input:
    aaa
    2
    2
    3
  2.  
  3. Output:
  4. aa
    aaa

Edited: Some input file contains garbage at the end. Do not process them.

建个后缀自动机之后,记录一下每个节点能到的节点数,然后询问的时候一遍dfs就行了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<cstring>
  6. #include<algorithm>
  7. #define ll long long
  8. #define maxn 400005
  9. using namespace std;
  10. int f[maxn],ch[maxn][26];
  11. int l[maxn],siz[maxn],n;
  12. int m,q,pre=1,cnt=1,rt[maxn];
  13. int a[maxn],c[maxn],tot[maxn];
  14. char s[maxn];
  15.  
  16. inline void ins(int x,int y){
  17. int p=pre,np=++cnt;
  18. pre=np,l[np]=l[p]+1;
  19. rt[np]=y+1;
  20.  
  21. for(;p&&!ch[p][x];p=f[p]) ch[p][x]=np;
  22. if(!p) f[np]=1;
  23. else{
  24. int q=ch[p][x];
  25. if(l[q]==l[p]+1) f[np]=q;
  26. else{
  27. int nq=++cnt;
  28. l[nq]=l[p]+1;
  29. memcpy(ch[nq],ch[q],sizeof(ch[q]));
  30. f[nq]=f[q];
  31. f[q]=f[np]=nq;
  32. for(;ch[p][x]==q;p=f[p]) ch[p][x]=nq;
  33. }
  34. }
  35. }
  36.  
  37. void dfs(int x){
  38. tot[x]=siz[x]=1;
  39. for(int i=0;i<26;i++) if(ch[x][i]){
  40. if(!siz[ch[x][i]]) dfs(ch[x][i]);
  41. tot[x]+=tot[ch[x][i]];
  42. }
  43. }
  44.  
  45. inline void build(){
  46. n=strlen(s);
  47. for(int i=0;i<n;i++) ins(s[i]-'a',i);
  48. for(int i=1;i<=cnt;i++) c[l[i]]++;
  49. for(int i=n;i>=0;i--) c[i]+=c[i+1];
  50. for(int i=1;i<=cnt;i++) a[c[l[i]]--]=i;
  51. for(int i=1;i<=cnt;i++){
  52. int now=a[i];
  53. rt[f[now]]=max(rt[f[now]],rt[now]);
  54. }
  55.  
  56. dfs(1);
  57. // for(int i=1;i<=cnt;i++) cout<<tot[i]<<' '<<siz[i]<<endl;
  58. }
  59.  
  60. void dfs2(int x){
  61. if(m<=siz[x]){
  62. m=0,puts("");
  63. return;
  64. }
  65.  
  66. m-=siz[x];
  67.  
  68. for(int i=0;i<26;i++){
  69. int to=ch[x][i];
  70. if(m<=tot[to]) putchar('a'+i),dfs2(to);
  71. else m-=tot[to];
  72.  
  73. if(!m) return;
  74. }
  75. }
  76.  
  77. inline void solve(){
  78. dfs2(1);
  79. }
  80.  
  81. int main(){
  82. scanf("%s",s);
  83. build();
  84.  
  85. scanf("%d",&q);
  86. while(q--){
  87. scanf("%d",&m),m++;
  88. solve();
  89. }
  90.  
  91. return 0;
  92. }

  

Spoj SUBLEX - Lexicographical Substring Search的更多相关文章

  1. SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组

    SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...

  2. SPOJ SUBLEX Lexicographical Substring Search - 后缀数组

    题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] ...

  3. spoj SUBLEX (Lexicographical Substring Search) RE的欢迎来看看

    SPOJ.com - Problem SUBLEX 这么裸的一个SAM,放在了死破OJ上面就是个坑. 注意用SAM做的时候输出要用一个数组存下来,然后再puts,不然一个一个字符输出会更慢. 还有一个 ...

  4. spoj SUBLEX - Lexicographical Substring Search【SAM】

    先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...

  5. spoj 7258 Lexicographical Substring Search (后缀自动机)

    spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...

  6. SPOJ:SUBLEX - Lexicographical Substring Search

    题面 第一行给定主串\((len<=90000)\) 第二行给定询问个数\(T<=500\) 随后给出\(T\)行\(T\)个询问,每次询问排名第\(k\)小的串,范围在\(int\)内 ...

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

    [题目链接] http://www.spoj.com/problems/SUBLEX/ [题目大意] 给出一个字符串,求其字典序排名第k的子串 [题解] 求出sam上每个节点被经过的次数,然后采用权值 ...

  8. ●SPOJ 7258 Lexicographical Substring Search

    题链: http://www.spoj.com/problems/SUBLEX/题解: 后缀自动机. 首先,因为相同的子串都被存在了自动机的同一个状态里面,所以这就很自然的避免了重复子串的问题. 然后 ...

  9. SPOJ 7258 Lexicographical Substring Search

    Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...

随机推荐

  1. python与execl的读写

    [code=python] #复制execl中的部分数据到另一份execl中的相同位置 import xlrd import xlwt import xlutils.copy path1=" ...

  2. leetcode 208. 实现 Trie (前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...

  3. LDA和PCA降维的原理和区别

     LDA算法的主要优点有: 在降维过程中可以使用类别的先验知识经验,而像PCA这样的无监督学习则无法使用类别先验知识. LDA在样本分类信息依赖均值而不是方差的时候,比PCA之类的算法较优. LDA算 ...

  4. 201621123034 《Java程序设计》第9周学习总结

    作业09-集合与泛型 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 答:Map的HashMap中使用嵌套类static class Node<K,V& ...

  5. 201621123033 《Java程序设计》第9周学习总结

    第九次作业 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 //stream(),filter(),collect() ...

  6. CSS——(2)盒子模型与标准流

    上篇博客<CSS--(1)基础>中简单介绍了CSS的概念和几种使用方法,现在主要是介绍其的核心内容. 盒子模型 为了理解盒子模型,我们可以先从生活中的盒子入手.盒子是用来放置物品的,内部除 ...

  7. 浅析Kerberos原理,及其应用和管理

    文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/5269739.html  转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...

  8. jqury关于cooke的操作写入cookie后只显示一次的DIV提示框代码

    有时候当用户登录系统后,需要给用户弹出提示框,但是不需要总是弹出来,在这里加入访问cookie来判断是否弹出过提示框,如果弹出过那么保存cookie,下次根据cookie是否存在来判断是否弹出 < ...

  9. [BZOJ4205][FJ2015集训] 卡牌配对 [建图+最大流]

    题面 这是bzoj权限题,题面可以去下面的离线题库找 离线4205,只有题面,不能提交 思路 二分图匹配 这道题模型显然就是个二分图匹配嘛 那我们两两判断一下然后连边匹配.....就只有30分了 因为 ...

  10. 【CF Round 439 A. The Artful Expedient】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...