Lexicographical Substrings Search

\[Time Limit: 149 ms \quad Memory Limit: 1572864 kB
\]

题意

给出一个字符串,求出这个字符串上字典序第 \(k\) 小的子串。

思路

先对给出的字符串构建后缀自动机,因为后缀自动机上从根节点走到任意节点都是原串的一个子串,所以我们可以 \(dfs\) 求出节点 \(i\) 往后存在多少个子串。

对于查询第 \(k\) 小的子串时,在用一个 \(dfs\) 来求,对于当前节点\(u\), 从\(1-26\) 枚举下一步的方案 \(v\),如果从 \(v\) 往后的子串比 \(k\) 少,那么不用往下走,直接 \(k-=cnt[v]\)。否则往下走一步,让 \(k--\)。如此一直走到 \(k=0\) 为止。

/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2019年05月22日 星期三 17时06分12秒
***************************************************************/ #include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int> typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m;
int cas, tol, T; struct SAM {
struct Node{
int next[27];
int fa, len;
void init() {
mes(next, 0);
len = fa = 0;
}
} node[maxn<<1];
int sz, last;
int cnt[maxn<<1];
void init() {
sz = last = 1;
node[sz].init();
mes(cnt, 0);
}
void insert(int k) {
int p = last, np = last = ++sz;
node[np].init();
node[np].len = node[p].len+1;
for(; p&&!node[p].next[k]; p=node[p].fa)
node[p].next[k] = np;
if(p==0) {
node[np].fa = 1;
} else {
int q = node[p].next[k];
if(node[q].len == node[p].len + 1) {
node[np].fa = q;
} else {
int nq = ++sz;
node[nq] = node[q];
node[nq].len = node[p].len + 1;
node[np].fa = node[q].fa = nq;
for(; p&&node[p].next[k]==q; p=node[p].fa)
node[p].next[k] = nq;
}
}
}
void dfs(int u) {
if(cnt[u]) return ;
cnt[u] = 1;
for(int i=1; i<=26; i++) {
int v = node[u].next[i];
if(v) {
dfs(v);
cnt[u] += cnt[v];
}
}
}
char ss[maxn];
void find(int u, int k, int len) {
if(k==0) {
ss[len] = '\0';
printf("%s\n", ss+1);
return ;
}
for(int i=1; i<=26; i++) {
int v = node[u].next[i];
if(v==0) continue;
if(cnt[v] < k) k -= cnt[v];
else {
k--;
ss[len] = 'a'+i-1;
find(v, k, len+1);
return ;
}
}
}
void solve(int k) {
find(1, k, 1);
}
} sam;
char s[maxn]; int main() {
sam.init();
scanf("%s", s+1);
int len = strlen(s+1);
for(int i=1; i<=len; i++) {
sam.insert(s[i]-'a'+1);
}
sam.dfs(1);
// for(int i=2; i<=sam.sz; i++) {
// printf("cnt[%d] = %d\n", i, sam.cnt[i]);
// }
scanf("%d", &T);
while(T--) {
scanf("%d", &m);
sam.solve(m);
}
return 0;
}

Lexicographical Substring Search SPOJ - SUBLEX (后缀自动机)的更多相关文章

  1. Lexicographical Substring Search SPOJ - SUBLEX (后缀数组)

    Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串 ...

  2. Lexicographical Substring Search (spoj7259) (sam(后缀自动机)+第k小子串)

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

  3. SPOJ - SUBLEX 后缀自动机

    SPOJ - SUBLEX 思路:求第k大字串,求出sam上每个节点开始能识别多少字串,然后从起点开始跑就好啦. #include<bits/stdc++.h> #define LL lo ...

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

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

  5. spoj 7258 Lexicographical Substring Search (后缀自动机)

    spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...

  6. 【SPOJ】7258. Lexicographical Substring Search(后缀自动机)

    http://www.spoj.com/problems/SUBLEX/ 后缀自动机系列完成QAQ...撒花..明天or今晚写个小结? 首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一 ...

  7. SPOJ SUBLEX 7258. Lexicographical Substring Search

    看起来像是普通的SAM+dfs...但SPOJ太慢了......倒腾了一个晚上不是WA 就是RE ..... 最后换SA写了...... Lexicographical Substring Searc ...

  8. [SPOJ7258]Lexicographical Substring Search

    [SPOJ7258]Lexicographical Substring Search 试题描述 Little Daniel loves to play with strings! He always ...

  9. 【SPOJ - SUBLEX】Lexicographical Substring Search 【后缀自动机+dp】

    题意 给出一个字符串和q个询问,每个询问给出一个整数k,输出第k大得子串. 分析 建后缀自动机,利用匹配边来解决.设d[v]为从状态v开始有多少不同的路径.这个显然是可以递推出来的.然后对于每个询问, ...

随机推荐

  1. Mysql 数据库 表中列的操作

    [1]Mysql数据库中表的列操作 Mysql中关于表中列的操作集语句: -- [1]增加一列 ) DEFAULT NULL COMMENT '目的码区号'; -- [2]增加一列,在dnis_are ...

  2. ubuntu docker 搭建 mongodb,开启授权访问 redis,mysql mssql 备份还原

    命令安装docker 如果您想从Ubuntu存储库安装docker版本,则可以运行下面的apt命令. sudo apt install docker.io等到安装完成后,您可以启动Docker并使用s ...

  3. [CF852E]Casinos and travel(2019-11-15考试)

    题目大意 有一棵\(n\)个点的树,令\(f(u)\)表示给树黑白染色,满足以\(u\)为根的树中,每个叶子节点到根的路径上黑点数量为偶数的染色方案数,求\(\sum\limits_{u=1}^n f ...

  4. 集合单列--Colletion

    集合 学习集合的目标: 会使用集合存储数据 会遍历集合,把数据取出来 掌握每种集合的特性 集合和数组的区别 数组的长度是固定的.集合的长度是可变的. 数组中存储的是同一类型的元素,可以存储基本数据类型 ...

  5. LaTeX转义特殊符号

    转义字符在LaTeX中有一些符号被用于特殊的用途,如 \\      \backslash\ 符号被用于命令的转义,直接在LaTeX中输入这些符号是无法正确得到这些符号的,甚至会引起LaTeX的报错. ...

  6. @interface __ annotation 子类可以继承到父类上的注解吗--有结论了

    博客分类: Java/J2se   作者:赵磊 博客:http://elf8848.iteye.com 不了解注解基础知识的请先看<JDK 5 Annotation\注解\注释\自定义注解> ...

  7. Phenix.NET for CSLA & WF4,企业级、分布式、符合领域建模的OOP软件快速开发平台

    2014-5-20版本: Phenix.NET for CSLA & WF 开发平台: http://download.csdn.net/download/phenixiii/7390405 ...

  8. 更新.net core 3.0,dotnet ef命令无法使用的解决办法

    之前项目采用.net core 2.2 实现,今天更新vs2019,系统.net core也被升级到3.0,在cmd中使用dotnet ef命令出现 “无法执行,因为找不到指定的命令或文件.可能的原因 ...

  9. 将fileupload标签的值清空

    在开发中遇到了这样一个问题,在一个form表单中,有一个fileupload标签,新增,修改都是同一个form,当我第一次选择了上传文件路径,并且提交之后,第二次再使用这个form,这次没有选择上传文 ...

  10. Java3-5年经验面试题总结

    记录一下本次找工作所遇到的一些高频面试题,第一次找java工作,感觉比面试.net舒服多了,17年的时候出去找.net工作,由于在公司做的东西用到的技术少,除了mvc和ef,其他没啥问的,就追着项目问 ...