SPOJ 7258 Lexicographical Substring Search
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 题解:
这题磨人啊,SPOJ跑的又慢,时限又短,压了一晚上常。
思路如下:
SAM满足性质:dfs序每一步走出来的都是一个子串,且满足从小到大顺序,关键满足不重不漏.
那么根据性质可以想出:沿着SAM走k步就是k-th子串了.
考虑优化:
记录size[x]为x节点往下可以产生的子串个数,和上题类似可以拓扑序搞出来 然后如果小于rk那么就直接减去即可.
压常技巧:
1.rg inline 不解释
2.不知为何,代码中k的输入写在函数内就快了4倍 懂得可以评论下....
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
using namespace std;
const int N=,M=;
char s[M];int n=,cnt=,last=,cur=,fa[M],ch[M][],dis[M],size[M];
void build(int c){
last=cur;cur=++cnt;
int p=last;
dis[cur]=++n;
for(;p && !ch[p][c];p=fa[p])ch[p][c]=cur;
if(!p)fa[cur]=;
else{
int q=ch[p][c];
if(dis[q]==dis[p]+)fa[cur]=q;
else{
int nt=++cnt;
dis[nt]=dis[p]+;
memcpy(ch[nt],ch[q],sizeof(ch[q]));
fa[nt]=fa[q];fa[q]=fa[cur]=nt;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nt;
}
}
}
int sa[M];int c[M];
void Flr(){
int p;
for(RG int i=;i<=cnt;i++)c[dis[i]]++;
for(RG int i=;i<=n;i++)c[i]+=c[i-];
for(RG int i=cnt;i;i--)sa[c[dis[i]]--]=i;
for(RG int i=cnt;i;i--){
p=sa[i];
size[p]=;
for(int j=;j<=;j++)
size[p]+=size[ch[p][j]];
}
}
void dfs(){
RG int u,x=,rk;
scanf("%d",&rk);
while(rk)
{
for(int i=;i<=;i++){
u=ch[x][i];
if(!u)continue;
if(size[u]>=rk){
putchar('a'+i);
x=u;rk--;break;
}
else rk-=size[u];
}
}
puts("");
}
void work(){
scanf("%s",s);
for(RG int i=,l=strlen(s);i<l;i++)build(s[i]-'a');
Flr();
int Q,x;
scanf("%d",&Q);
while(Q--)dfs();
}
int main()
{
work();
return ;
}
SPOJ 7258 Lexicographical Substring Search的更多相关文章
- spoj 7258 Lexicographical Substring Search (后缀自动机)
spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...
- 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 [后缀自动机 DP]
题意:给一个长度不超过90000的串S,每次询问它的所有不同子串中,字典序第K小的,询问不超过500个. 第一道自己做的1A的SAM啦啦啦 很简单,建SAM后跑kth就行了 也需要按val基数排序倒着 ...
- SPOJ SUBLEX 7258. Lexicographical Substring Search
看起来像是普通的SAM+dfs...但SPOJ太慢了......倒腾了一个晚上不是WA 就是RE ..... 最后换SA写了...... Lexicographical Substring Searc ...
- SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
- 【SPOJ】7258. Lexicographical Substring Search(后缀自动机)
http://www.spoj.com/problems/SUBLEX/ 后缀自动机系列完成QAQ...撒花..明天or今晚写个小结? 首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一 ...
- 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,不然一个一个字符输出会更慢. 还有一个 ...
随机推荐
- 源端控制的OpenFlow数据面
OpenFlow 交换机一般采用 TCAM 存储和查找流表,从而带来了扩展性.成本和能耗的问题.TCAM 成本和能耗过高,存储容量有限,一般交换机中的 TCAM 仅能存储几千条流表项,对 OpenFl ...
- 翻译:CREATE FUNCTION语句(已提交到MariaDB官方手册)
本文为mariadb官方手册:CREATE FUNCTION的译文. 原文:https://mariadb.com/kb/en/library/create-function/我提交到MariaDB官 ...
- JAVA_SE基础——62.String类的构造方法
下面我先列出初学者目前用到的构造方法 String 的构造方法: String() 创建一个空内容 的字符串对象. String(byte[] bytes) 使用一个字节数组构建一个字 ...
- JAVA_SE基础——19.数组的定义
数组是一组相关数据的集合,数组按照使用可以分为一维数组.二维数组.多维数组 本章先讲一维数组 不同点: 不使用数组定义100个整形变量:int1,int2,int3;;;;;; 使用数组定义 int ...
- php中函数和方法的区别
php的方法就是定义在类里面的方法,一般不建议在方法内部定义方法,但是这种也可以这种叫做内部方法,一般只能本方法调用. 如果定义在同一个类中的方法,在同类的其他方法中调用是$this->方法名就 ...
- js前端对后台数据的获取,如果是汉字则需要添上引号
js前端对后台数据的获取,如果是汉字则需要添上引号
- hadoop2.6.0实践:003 检查hadoop是否可用
start-dfs.sh start-yarn.sh 1.检查hdfs hdfs dfs -ls / http://localhost:50070 2.运行例子程序 hdfs dfs -ls / hd ...
- spring-oauth-server实践:授权方式三:PASSWORD模式下 authorities:ROLE_{user.privillege}, ROLE_USER
一.数据库配置 1.oauth_client_details 2.user_ 3.user_privillege 二.password模式 授权过程 1.授权者granter和请求参数 Resourc ...
- Spark入门(1-2)Spark的特点、生态系统和技术架构
一.Spark的特点 Spark特性 Spark通过在数据处理过程中成本更低的洗牌(Shuffle)方式,将MapReduce提升到一个更高的层次.利用内存数据存储和接近实时的处理能力,Spark比其 ...
- 如何修改chrome记住密码后自动填充表单的黄色背景 ?
input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill { background-color: rgb(2 ...