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');
}
}

Lexicographical Substring Search (spoj7259) (sam(后缀自动机)+第k小子串)的更多相关文章

  1. LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  2. spoj SUBLEX - Lexicographical Substring Search【SAM】

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

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

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

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

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

  5. [SPOJ7258]Lexicographical Substring Search

    [SPOJ7258]Lexicographical Substring Search 试题描述 Little Daniel loves to play with strings! He always ...

  6. SPOJ SUBLEX 7258. Lexicographical Substring Search

    看起来像是普通的SAM+dfs...但SPOJ太慢了......倒腾了一个晚上不是WA 就是RE ..... 最后换SA写了...... Lexicographical Substring Searc ...

  7. 弦论(tjoi2015,bzoj3998)(sam(后缀自动机))

    对于一个给定长度为\(N\)的字符串,求它的第\(K\)小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串\(S\) 第二行为两个整数\(T\)和\(K\),\(T\)为0则表示不同 ...

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

    Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串 ...

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

    http://www.spoj.com/problems/SUBLEX/ 后缀自动机系列完成QAQ...撒花..明天or今晚写个小结? 首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一 ...

随机推荐

  1. (转 留存)Windows环境下的NodeJS+NPM+GIT+Bower安装配置步骤

    Windows环境下的NodeJS+NPM+GIT+Bower安装配置步骤 标签: NodeJSnpmbower 2015-07-17 16:38 3016人阅读 评论(0) 收藏 举报  分类: G ...

  2. 【常见CPU架构对比】维基百科

    Comparison of instruction set architectures https://en.wikipedia.org/wiki/Comparison_of_instruction_ ...

  3. 若p是与10互质的质数,则p-1个9能被p整除

    [若p是与10互质的质数,则k(p-1)个9能被p整除] 因为(p,10)=1,所以(p,10^k)=1.根据费马定理,10^(k*(p-1))-1|p. 而10^k*(p-1)-1是一个位数为(p- ...

  4. oracle中如何修改process

    转自https://blog.csdn.net/qq_35686181/article/details/52350922 oracle中修改process  在 oracle中,要经常查看proces ...

  5. OPENSSL 生成https 客户端证书

    下面说下拿服务器证书.(前提是服务器是https,客户端认证用的时候),服务端不给的时候,我们自己去拿(不给怼他!,哈哈,开个玩笑,都会给的) openssl s_client -connect 域名 ...

  6. JavaScript RegExp.exec() 方法

    定义和用法: exec() 方法用于检索字符串中的正则表达式的匹配. 语法: RegExpObject.exec(string); RegExpObject:必须参数,正则表达式: string:必须 ...

  7. 第六章 Windows应用程序对键盘与鼠标的响应 P121 6-8

    基于键盘与鼠标应用的程序设计 一.实验目的 1.掌握键盘与鼠标在应用程序中的消息响应机制.   二.实验内容及步骤 实验任务 1.熟悉键盘的消息响应: 2.熟悉鼠标的消息响应: 实验内容 设计一个窗口 ...

  8. 2018年3大UI设计趋势,你知道吗?

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 之前小编已经和大家讨论了2018年软件测试的五大趋势,现在让我们一起来看看移动UI设计在2018年会 ...

  9. etl业务验证方法1

    /* Copyright (c) 2015 Xiamen Weixin Software Co., Ltd. All rights reserved *  * Create by huanglc@ho ...

  10. 475. Heaters

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...