Spoj SUBLEX - Lexicographical Substring Search
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
Input:
aaa
2
2
3 Output:
aa
aaa
Edited: Some input file contains garbage at the end. Do not process them.
建个后缀自动机之后,记录一下每个节点能到的节点数,然后询问的时候一遍dfs就行了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
#define maxn 400005
using namespace std;
int f[maxn],ch[maxn][26];
int l[maxn],siz[maxn],n;
int m,q,pre=1,cnt=1,rt[maxn];
int a[maxn],c[maxn],tot[maxn];
char s[maxn]; inline void ins(int x,int y){
int p=pre,np=++cnt;
pre=np,l[np]=l[p]+1;
rt[np]=y+1; for(;p&&!ch[p][x];p=f[p]) ch[p][x]=np;
if(!p) f[np]=1;
else{
int q=ch[p][x];
if(l[q]==l[p]+1) f[np]=q;
else{
int nq=++cnt;
l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
f[nq]=f[q];
f[q]=f[np]=nq;
for(;ch[p][x]==q;p=f[p]) ch[p][x]=nq;
}
}
} void dfs(int x){
tot[x]=siz[x]=1;
for(int i=0;i<26;i++) if(ch[x][i]){
if(!siz[ch[x][i]]) dfs(ch[x][i]);
tot[x]+=tot[ch[x][i]];
}
} inline void build(){
n=strlen(s);
for(int i=0;i<n;i++) ins(s[i]-'a',i);
for(int i=1;i<=cnt;i++) c[l[i]]++;
for(int i=n;i>=0;i--) c[i]+=c[i+1];
for(int i=1;i<=cnt;i++) a[c[l[i]]--]=i;
for(int i=1;i<=cnt;i++){
int now=a[i];
rt[f[now]]=max(rt[f[now]],rt[now]);
} dfs(1);
// for(int i=1;i<=cnt;i++) cout<<tot[i]<<' '<<siz[i]<<endl;
} void dfs2(int x){
if(m<=siz[x]){
m=0,puts("");
return;
} m-=siz[x]; for(int i=0;i<26;i++){
int to=ch[x][i];
if(m<=tot[to]) putchar('a'+i),dfs2(to);
else m-=tot[to]; if(!m) return;
}
} inline void solve(){
dfs2(1);
} int main(){
scanf("%s",s);
build(); scanf("%d",&q);
while(q--){
scanf("%d",&m),m++;
solve();
} return 0;
}
Spoj SUBLEX - Lexicographical Substring Search的更多相关文章
- SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
- SPOJ SUBLEX Lexicographical Substring Search - 后缀数组
题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] ...
- spoj SUBLEX (Lexicographical Substring Search) RE的欢迎来看看
SPOJ.com - Problem SUBLEX 这么裸的一个SAM,放在了死破OJ上面就是个坑. 注意用SAM做的时候输出要用一个数组存下来,然后再puts,不然一个一个字符输出会更慢. 还有一个 ...
- spoj SUBLEX - Lexicographical Substring Search【SAM】
先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...
- spoj 7258 Lexicographical Substring Search (后缀自动机)
spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...
- SPOJ:SUBLEX - Lexicographical Substring Search
题面 第一行给定主串\((len<=90000)\) 第二行给定询问个数\(T<=500\) 随后给出\(T\)行\(T\)个询问,每次询问排名第\(k\)小的串,范围在\(int\)内 ...
- SPOJ 7258 Lexicographical Substring Search(后缀自动机)
[题目链接] http://www.spoj.com/problems/SUBLEX/ [题目大意] 给出一个字符串,求其字典序排名第k的子串 [题解] 求出sam上每个节点被经过的次数,然后采用权值 ...
- ●SPOJ 7258 Lexicographical Substring Search
题链: http://www.spoj.com/problems/SUBLEX/题解: 后缀自动机. 首先,因为相同的子串都被存在了自动机的同一个状态里面,所以这就很自然的避免了重复子串的问题. 然后 ...
- SPOJ 7258 Lexicographical Substring Search
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
随机推荐
- python3知识点之---------字符串的介绍
1. 定义 其实字符串就是一系列字符,用引号括起来的就是字符串,其中的引号可以是单引号或者双引号. 比如 "This is a string" 'This is a strin ...
- SPOJ - DQUERY(区间不同数+树状数组)
链接:SPOJ - DQUERY 题意:求给定区间不同数的个数(不更新). 题解:离线+树状数组. 对所求的所有区间(l, r)根据r从小到大排序.从1-n依次遍历序列数组,在树状数组中不断更新a[i ...
- 设置hostname
由于 http://1.2.3.4 不是一个有效的 apt 源,安装肯定会失败,我们可以在 /var/log/cloud-init.log 看到失败的信息. cloud-init 默认会将 insta ...
- resharper激活
1.解压后点击64位系统的IntelliJIDEALicenseServer_windows_amd64.exe 32位点击IntelliJIDEALicenseServer_windows ...
- Java 中xml解析
1.String 字符串保持到txt文件 String xml ="abcdefghijk"; FileWriter fw = null; File f = new File(&q ...
- nginx教程全集汇总
Nginx基础1. nginx安装:httpwww.ttlsa.comnginxnginx-install-on-linux2. nginx 编译参数详解(运维不得不看):http://www.t ...
- Python中的返回函数与闭包
返回函数,顾名思义,就是高阶函数可以把函数作为return值返回.与闭包的关系是:闭包需要以返回函数的形式实现. 一. 返回函数 比如我们有一个求和函数: >>> def calc_ ...
- 浅谈android Socket 通信及自建ServerSocket服务端常见问题
摘 要:TCP/IP通信协议是可靠的面向连接的网络协议,它在通信两端各建立一个Socket,从而在两端形成网络虚拟链路,进而应用程序可通过可以通过虚拟链路进行通信.Java对于基于TCP协议的网络通 ...
- Android键盘面板冲突 布局闪动处理方案
转:来自Android键盘面板冲突 布局闪动处理方案 起源,之前在微信工作的时候,为了给用户带来更好的基础体验,做了很多尝试,踩了很多输入法的坑,特别是动态调整键盘高度,二级页面是透明背景,魅族早期的 ...
- 关于Bootstrap 利用radio实现tab切换的一个问题
1.html代码 <div class="col-sm-10 nav nav-tabs" id="typelist" role="tablist ...