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

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

题意:

给定一个字符串,求排名第k小的串。

题解:

把串塞进一个后缀自动机,在图上反向拓扑求出每个点的后继串的数量,然后像在权值线段树上找第\(k\)大一样找就行了。

#include<bits/stdc++.h>
using namespace std;
const int N=2000010;
char s[N];
int a[N],c[N],ans[N];
struct SAM{
int last,cnt;
int size[N],ch[N][26],fa[N<<1],l[N<<1];
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=1;
else{
int q=ch[p][c];
if(l[p]+1==l[q])fa[np]=q;
else{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
size[nq]=1;
}
}
size[np]=1;
}
void build(char s[]){
int len=strlen(s+1);
last=cnt=1;
for(int i=1;i<=len;++i)ins(s[i]-'a');
}
void calc(int len){
for(int i=1;i<=cnt;++i)c[l[i]]++;
for(int i=1;i<=cnt;++i)c[i]+=c[i-1];
for(int i=1;i<=cnt;++i)a[c[l[i]]--]=i;
for(int i=cnt;i;--i){
int p=a[i];
for(int j=0;j<26;++j){
size[p]+=size[ch[p][j]];
}
}
}
void find(int k){
int p=1;
while(k){
int a=0;
while(k>size[ch[p][a]]&&a<26){
if (ch[p][a]) k-=size[ch[p][a]];
a++;
}
putchar('a'+a);k--;
p=ch[p][a];
}
}
}sam;
int main(){
cin>>s+1;
sam.build(s);
sam.calc(strlen(s+1));
int t;
cin>>t;
while(t--){
int x;
scanf("%d",&x);
sam.find(x);putchar('\n');
}
}

最新文章

  1. swagger for c# webapi
  2. du -sg 和df -g 所看的文件系统大小不一致
  3. [PE结构分析] 10.基址重定位
  4. [转]Eclipse Java注释模板设置详解
  5. Winform开发框架之权限管理系统
  6. POJ 1287 Networking
  7. iOS之即时通讯相关理解
  8. 关于Webapp的注意事项
  9. 理解php的opcode
  10. win7(windows 7)系统下安装SQL2005(SQL Server 2005)图文教程
  11. 安卓高级6 CoordinatorLayout
  12. JDBC(10)—批处理
  13. ThreadLocal的学习
  14. python爬虫-基础入门-爬取整个网站《1》
  15. 15、Redis的集群
  16. ibatis的缓存机制
  17. [转]Tornado get/post请求异步处理框架分析
  18. 如何移动 nuget 缓存文件夹
  19. 13年10月 月赛第一场 set 4 迷宫问题
  20. 与R纠缠的两件事——rownames和子集--转载

热门文章

  1. Openstack 组件简介
  2. 新手C#构造函数、继承、组合的学习2018.08.06/07
  3. go_接口
  4. 为什么数组没有实现Iterable接口,但可以使用foreach语句遍历
  5. 6-关于#include&lt;bits/stdc++.h&gt;
  6. QTcpSocket-Qt使用Tcp通讯实现服务端和客户端
  7. myeclipse 快捷键,从步骤开始的大括号定位到匹配方法结束的大括号
  8. RocketMQ 运维指令
  9. Centos环境下手动设置-网络参数配置-网络挨排错顺序-设置网卡为上网模式的设定
  10. [leetcode] 9. Binary Tree Level Order Traversal