SPOJ7258 SUBLEX - 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
Edited: Some input file contains garbage at the end. Do not process them.
会做但是不会写qwq,
思路很简单:
把SAM的转移边形成的DAG图求出来,然后跟主席树查第$k$大一样,贪心枚举每一位,这个节点的某个儿子的大小大于$k$了,说明要找的串在这个儿子里,
否则就把$k$减去节点大小,继续找
一开始弄不清楚DAG图求出来的$size$和前缀树求出来的$size$有啥区别。
大概就是:
对于每个节点来说,DAG图求出来的$size$表示有多少以该节点代表的字符为起点的子串
前缀树求出来的$size$表示该节点所代表状态的$right$集合大小是多少,也就是该状态出现了多少次
这题居然卡dfs mmp
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = 180001;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
char s[MAXN];
int opt, K, N;
int fa[MAXN], len[MAXN], ch[MAXN][27], siz[MAXN], tot = 1, last = 1, root = 1;
void insert(int x) {
int now = ++tot, pre = last; last = now; len[now] = len[pre] + 1;
siz[now] = 1;
for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
if(!pre) fa[now] = root;
else {
int q = ch[pre][x];
if(len[q] == len[pre] + 1) fa[now] = q;
else {
int nows = ++tot; len[nows] = len[pre] + 1;
memcpy(ch[nows], ch[q], sizeof(ch[q]));
fa[nows] = fa[q]; fa[q] = fa[now] = nows;
for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nows;
}
}
}
void Query(int K) {
int now = root;
while(K) {
if(now != root) K--;
if(K <= 0) break;
for(int i = 0; i <= 25; i++)
if(ch[now][i]) {
if(siz[ch[now][i]] >= K) {putchar(i + 'a'); now = ch[now][i]; break; }
else K -= siz[ch[now][i]];
}
}
puts("");
}
void Topsort() {
static int A[MAXN], a[MAXN];
for(int i = 1; i <= tot; i++) A[len[i]]++;
for(int i = 1; i <= N; i++) A[i] += A[i - 1];
for(int i = tot; i >= 1; i--) a[A[len[i]]--] = i;
for(int i = 1; i <= tot; i++) siz[i] = 1;
for(int i = tot; i ; i--)
for(int j = 0; j <= 25; j++)
siz[a[i]] += siz[ch[a[i]][j]];
}
int main() {
scanf("%s", s + 1);
N = strlen(s + 1);
for(int i = 1; i <= N; i++) insert(s[i] - 'a');
Topsort();
int T = read();
while(T--) {
int K = read();
Query(K);
}
return 0;
}
- SQL连接查询(最全面)
- Java Beans
- 呐你们要的算法(一)No.17
- 使用Python生成一张用于登陆验证的字符图片
- Jupyter notebook的安装方法
- Linux学习第三十四篇:rsync通过服务同步,Linux系统日志,screen工具
- 小程序支付详解+源码(客户端+服务端)
- Python Windows下分布式进程的坑(分布式进程的一个简单例子)
- Python pdb调试
- linux学习第三十五篇:LAMP架构介绍,MySQL_MariaDB介绍,MySQL安装
- 尝试克服一下小伙伴对神经网络的恐惧No.26
- Python 元类(MetaClass)
- linux学习第三十六篇:MariaDB安装,Apache安装
- Python 5种连接字符串的方法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Go 项目最佳实践
- 虚拟机更换JDK版本步骤(Hadoop集群)
- JavaSE重点复习
- [数据结构与算法] 树结构之二叉排序树、平衡二叉树、多路查找树
- 工作后, 你一定不能错过技术之JDK1.8的新特性
- 【Go】剑指offer:二叉树子树的判断
- css中的box-shadow属性详解
- 基于docker快速搭建多节点Hadoop集群
- CSS简笔画:纯CSS绘制一辆婴儿车
- 分布式自增数据库ID
- 【STM32F429开发板用户手册】第27章 STM32F429的定时器应用之TIM1-TIM14的PWM实现
- 数据库基础开源学习教程-android 使用 litepal 操作本地数据库
- 红黑树——动态+静态图
- 一文读懂Python实现张量运算
- javascript之闭包基础了解