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 ...
随机推荐
- python之if测试
(一)python的条件判断语句一般格式如下: if (条件1): (执行结果) elif(条件2): (执行结果) ..... else: (执行结果) 执行顺序为从上到下判断,若条件1不符合则进入 ...
- Python全栈工程师(数值类型、运算符)
ParisGabriel Python 入门基础 python的应用领域: 1.系统运维 2.网络编程(如:网络爬虫,搜索引擎,服务器编程) 3.科学计算 4.航空领域(如:卫星, ...
- [译]如何将docker日志重定向到单个文件里
原文来源: how-to-redirect-docker-logs-to-a-single-file 问题: 我想把某一个docker的log全部导出到一个文件里进行分析,我该怎么做? 其实不用那样操 ...
- java安全提交笔记【xmind图片】
- linux进程——后台运行的方法
linux进程后台运行的几种方法: 我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败. 如何让命 ...
- Installing Wine 1.5: configure: error: Cannot build a 32-bit program, you need to install 32-bit development libraries(转载)
Installing Wine 1.5: configure: error: Cannot build a 32-bit program, you need to install 32-bit dev ...
- MySQL数据库从windows迁移到linux
前几天搭建了lamp环境,想把之前写的小东西迁到linux上运行,涉及到把mysql数据库的文件迁移到linux上,直接用fileZilla传过去应该不行,我试了下,反正没成功.下面是我采用的方法: ...
- HDU 1841 Find the Shortest Common Superstring----KMP
题意:给两个字符串,问包含这两个字符串的最小的字符串的长度 kmp返回匹配串长度 #include "iostream" #include<cstdio> #inclu ...
- PHP AES128加密解密
<?php /** * Class AES */ class AES { public static function encrypt($input, $key) { $size = mcryp ...
- 关于each()、find()、filter()遍历节点的操作方法
关于each().find().filter()遍历节点的操作方法 each语法: each(fn) ; 返回值:jQuery fn:代表对于每个匹配元素所要执行的函数 each()方法共有三种形式 ...