\(\color{#0066ff}{ 题目描述 }\)

给定一个字符串,求排名第k小的串

\(\color{#0066ff}{输入格式}\)

第一行给定主串(len<=90000)

第二行给定询问个数T<=500

随后给出T行T个询问,每次询问排名第k小的串,范围在int内

\(\color{#0066ff}{输出格式}\)

对于每一个询问,输出T行,每行为排名第k小的串

\(\color{#0066ff}{输入样例}\)

aaa
2
2
3

\(\color{#0066ff}{输出样例}\)

aa
aaa

\(\color{#0066ff}{数据范围与提示}\)

none

\(\color{#0066ff}{ 题解 }\)

求第k小,考虑在Sam上贪心

这时我们需要的siz是节点,所以统一dfs处理(注意记忆化)

然后依次贪心就行了

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
char ch; int x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 2e5 + 5;
struct SAM {
protected:
struct node {
node *ch[26], *fa;
int len, siz;
node(int len = 0, int siz = 0): fa(NULL), len(len), siz(siz) {
memset(ch, 0, sizeof ch);
}
};
node *root, *tail, *lst;
node pool[maxn];
void extend(int c) {
node *o = new(tail++) node(lst->len + 1), *v = lst;
for(; v && !v->ch[c]; v = v->fa) v->ch[c] = o;
if(!v) o->fa = root;
else if(v->len + 1 == v->ch[c]->len) o->fa = v->ch[c];
else {
node *n = new(tail++) node(v->len + 1), *d = v->ch[c];
std::copy(d->ch, d->ch + 26, n->ch);
n->fa = d->fa, d->fa = o->fa = n;
for(; v && v->ch[c] == d; v = v->fa) v->ch[c] = n;
}
lst = o;
}
void clr() {
tail = pool;
root = lst = new(tail++) node();
}
void getans(int k, node *o) {
if(!k) return;
for(int i = 0; i <= 25; i++) {
if(o->ch[i]) {
if(o->ch[i]->siz >= k) {
putchar((char)(i + 'a'));
getans(k - 1, o->ch[i]);
return;
}
else k -= o->ch[i]->siz;
}
}
}
void getsiz(node *o) {
if(o->siz) return;
o->siz = 1;
for(int i = 0; i <= 25; i++) if(o->ch[i]) getsiz(o->ch[i]), o->siz += o->ch[i]->siz;
}
public:
SAM() { clr(); }
void ins(char *s) { for(char *p = s; *p; p++) extend(*p - 'a'); }
void getsiz() { getsiz(root); }
void getans(int k) { getans(k, root); }
}sam;
char s[maxn];
int main() {
scanf("%s", s);
sam.ins(s);
sam.getsiz();
for(int T = in(); T --> 0;) {
int k = in();
sam.getans(k);
puts("");
}
return 0;
}

SP7258 SUBLEX - Lexicographical Substring Search的更多相关文章

  1. SP7258 SUBLEX - Lexicographical Substring Search(后缀自动机)

    传送门 解题思路 首先建\(sam\),然后在拓扑序上\(dp\)一下,把每个点的路径数算出来,然后统计答案时就在自动机上\(dfs\)一下,仿照平衡树那样找第\(k\)小. 代码 #include& ...

  2. SP7258 SUBLEX - Lexicographical Substring Search - 后缀自动机,dp

    给定一个字符串,求本质不同排名第k小的子串 Solution 后缀自动机上每条路径对应一个本质不同的子串 按照 TRANS 图的拓扑序,DP 计算出每个点发出多少条路径 (注意区别 TRANS 图的拓 ...

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

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

  4. SPOJ SUBLEX Lexicographical Substring Search - 后缀数组

    题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] ...

  5. spoj SUBLEX (Lexicographical Substring Search) RE的欢迎来看看

    SPOJ.com - Problem SUBLEX 这么裸的一个SAM,放在了死破OJ上面就是个坑. 注意用SAM做的时候输出要用一个数组存下来,然后再puts,不然一个一个字符输出会更慢. 还有一个 ...

  6. SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀自动机)

    Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...

  7. SPOJ:SUBLEX - Lexicographical Substring Search

    题面 第一行给定主串\((len<=90000)\) 第二行给定询问个数\(T<=500\) 随后给出\(T\)行\(T\)个询问,每次询问排名第\(k\)小的串,范围在\(int\)内 ...

  8. Spoj SUBLEX - Lexicographical Substring Search

    Dicription Little Daniel loves to play with strings! He always finds different ways to have fun with ...

  9. spoj SUBLEX - Lexicographical Substring Search【SAM】

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

随机推荐

  1. Ubuntu16.04+TensorFlow r1.12环境搭建指南

    一.操作系统安装 OS版本:Ubuntu 16.04 (ubuntu-16.04.5-server-amd64.iso) CPU:4Core以上 内存:4GB以上 磁盘空间:80G以上 二.基础环境准 ...

  2. 关于python+django操作数据库中的表

    数据库中的表示这样设计的 class C(models.Model): name = models.CharField(max_length=32) class B(models.Model): na ...

  3. python 面向对象之反射及内置方法

    面向对象之反射及内置方法 一.静态方法(staticmethod)和类方法(classmethod) 类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性) 静 ...

  4. Mysql数据库服务器配置文件/etc/my.cnf的详细配置

    以下是 Mysql数 据库服务器配置文件 /etc/my.cnf的详细配置.应用场合是 InnoDB引擎, 4核 CPU, 32位SUSE.   [client] port        = 3306 ...

  5. spring的配置文件在web.xml中加载的方式

    web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且 ...

  6. LNMP 1.5 php-fpm配置文件

    php-fpm配置文件: /usr/local/php/etc/php-fpm.conf    :php-fpm服务的配置文件 /usr/local/php/etc/php.ini       :ph ...

  7. 【转载】基于TINY4412的Andorid开发-------简单的LED灯控制

    阅读目录(Content) 一.编写驱动程序 二.编写代码测试驱动程序 三.编写HAL代码 四.编写Framework代码 五.编写JNI代码 六.编写App 参考资料: <Andriod系统源 ...

  8. Composite模式 组合模式

    Android的ViewGroup 和 View 的关系,即是采用组合模式 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件 ...

  9. Jsonp实现跨域请求Ajax

    客户端 #!/usr/bin/env python import tornado.ioloop import tornado.web class MainHandler(tornado.web.Req ...

  10. day36-hibernate检索和优化 02-Hibernate检索方式:简单查询及别名查询

    Hibernate:     insert     into        Customer        (cname)     values        (?)Hibernate:     in ...