SP7258 SUBLEX - Lexicographical Substring Search
\(\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的更多相关文章
- SP7258 SUBLEX - Lexicographical Substring Search(后缀自动机)
传送门 解题思路 首先建\(sam\),然后在拓扑序上\(dp\)一下,把每个点的路径数算出来,然后统计答案时就在自动机上\(dfs\)一下,仿照平衡树那样找第\(k\)小. 代码 #include& ...
- SP7258 SUBLEX - Lexicographical Substring Search - 后缀自动机,dp
给定一个字符串,求本质不同排名第k小的子串 Solution 后缀自动机上每条路径对应一个本质不同的子串 按照 TRANS 图的拓扑序,DP 计算出每个点发出多少条路径 (注意区别 TRANS 图的拓 ...
- 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,不然一个一个字符输出会更慢. 还有一个 ...
- SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀自动机)
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- SPOJ:SUBLEX - Lexicographical Substring Search
题面 第一行给定主串\((len<=90000)\) 第二行给定询问个数\(T<=500\) 随后给出\(T\)行\(T\)个询问,每次询问排名第\(k\)小的串,范围在\(int\)内 ...
- Spoj SUBLEX - Lexicographical Substring Search
Dicription Little Daniel loves to play with strings! He always finds different ways to have fun with ...
- spoj SUBLEX - Lexicographical Substring Search【SAM】
先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...
随机推荐
- ManualResetEvent 用法
第一.简单介绍 ManualResetEvent 允许线程通过发信号互相通信.通常,此通信涉及一个线程在其他线程进行之前必须完成的任务.当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时, ...
- [Chapter 3 Process]Practice 3.2 Including the initial parent process, how many processes are created by the program shown in Figure?
3.2 Including the initial parent process, how many processes are created by the program shown in Fig ...
- python_class21
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @version: 3.5 @author: morgana @lic ...
- JDBC批处理数据
JDBC3.0 的增强支持BLOB,CLOB,ARRAY,REF数据类型.的ResultSet对象UPDATEBLOB(),updateCLOB(),updateArray()和updateRef( ...
- 【Android 多媒体应用】使用 TTS
import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.speec ...
- xcode中用pods管理第三方库<转>
安装pods :http://www.cnblogs.com/wangluochong/p/5567082.html 史上最详细的CocoaPods安装教程 --------------------- ...
- 【273】利用ArcPy建立处理数据的脚本
这个脚本可以直接运行处理程序,首先在 ArcPy 上面测试,成功后写入文件,下面的代码实现将指定文件夹内部的栅格数据进行 Calculate Statistics 操作,否则在进行专题图制作的时候会出 ...
- c++ vector用法和迭代器
1.在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. (1)头文件#include<vector>. (2)创建vector对象,vector<int> ...
- struts-hibernate整合(1)配置环境
①加载jar包 创建类库: 在myeclipse中点击windows---Preference---Java---Build Path---User Libraries---new 输入创建类库名字s ...
- if __name__ == "__main__": 的使用
#!/usr/bin/env python from qq.lib.a2 import register from qq.lib.a3 import login def main(): while T ...