SP7258 SUBLEX - Lexicographical Substring Search - 后缀自动机,dp
给定一个字符串,求本质不同排名第k小的子串
Solution
后缀自动机上每条路径对应一个本质不同的子串
按照 TRANS 图的拓扑序,DP 计算出每个点发出多少条路径
(注意区别 TRANS 图的拓扑序和后缀链接的拓扑序)
然后暴力 26 分询问即可

#include <bits/stdc++.h>
using namespace std;
const int Maxn = 2000005;
struct Suffix_Automata {
int maxlen[Maxn], trans[Maxn][26], link[Maxn], Size, Last;
int t[Maxn], a[Maxn], cnt[Maxn], f[Maxn];
Suffix_Automata() { Size = Last = 1; }
inline void Extend(int id) {
int cur = (++ Size), p;
maxlen[cur] = maxlen[Last] + 1;
cnt[cur] = 1;
for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur;
if (!p) link[cur] = 1;
else {
int q = trans[p][id];
if (maxlen[q] == maxlen[p] + 1) link[cur] = q;
else {
int clone = (++ Size);
maxlen[clone] = maxlen[p] + 1;
for(int i=0;i<26;i++) trans[clone][i] = trans[q][i];
link[clone] = link[q];
for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone;
link[cur] = link[q] = clone;
}
}
Last = cur;
}
void CalcEndposSize() {
memset(t, 0, sizeof t);
for(int i=1; i<=Size; i++) t[maxlen[i]]++;
for(int i=1; i<=Size; i++) t[i]+=t[i-1];
for(int i=1; i<=Size; i++) a[t[maxlen[i]]--]=i;
/*for(int i=Size; i>=1; --i) cnt[link[a[i]]]+=cnt[a[i]];
cnt[1] = 0;*/
}
void DFS(int p) {
for(int i=0;i<26;i++) {
if(trans[p][i]) {
if(f[trans[p][i]]==0) DFS(trans[p][i]);
f[p]+=f[trans[p][i]];
}
}
f[p]+=cnt[p];
}
void Go(int p,int k) {
k-=cnt[p];
for(int i=0;i<26 && k>0;i++) {
if(trans[p][i]) {
if(f[trans[p][i]]>=k) {
cout<<(char)(i+'a');
Go(trans[p][i],k);
return;
}
else {
k-=f[trans[p][i]];
}
}
}
}
} sam;
int main() {
ios::sync_with_stdio(false);
string str;
cin>>str;
int t,k;
for(int i=0;i<str.length();i++)
sam.Extend(str[i]-'a');
sam.CalcEndposSize();
for(int i=2; i<=sam.Size; i++)
sam.cnt[i] = 1;
sam.DFS(1);
cin>>t;
while(t--) {
cin>>k;
sam.Go(1,k);
cout<<endl;
}
}
SP7258 SUBLEX - Lexicographical Substring Search - 后缀自动机,dp的更多相关文章
- SP7258 SUBLEX - Lexicographical Substring Search(后缀自动机)
传送门 解题思路 首先建\(sam\),然后在拓扑序上\(dp\)一下,把每个点的路径数算出来,然后统计答案时就在自动机上\(dfs\)一下,仿照平衡树那样找第\(k\)小. 代码 #include& ...
- SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
- SPOJ 7258 Lexicographical Substring Search [后缀自动机 DP]
题意:给一个长度不超过90000的串S,每次询问它的所有不同子串中,字典序第K小的,询问不超过500个. 第一道自己做的1A的SAM啦啦啦 很简单,建SAM后跑kth就行了 也需要按val基数排序倒着 ...
- SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀自动机)
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- spoj 7258 Lexicographical Substring Search (后缀自动机)
spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...
- SPOJ SUBLEX Lexicographical Substring Search - 后缀数组
题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] ...
- SPOJ Lexicographical Substring Search 后缀自动机
给你一个字符串,然后询问它第k小的factor,坑的地方在于spoj实在是太慢了,要加各种常数优化,字符集如果不压缩一下必t.. #pragma warning(disable:4996) #incl ...
- SP7258 SUBLEX - Lexicographical Substring Search
\(\color{#0066ff}{ 题目描述 }\) 给定一个字符串,求排名第k小的串 \(\color{#0066ff}{输入格式}\) 第一行给定主串(len<=90000) 第二行给定询 ...
- Spoj SUBLEX - Lexicographical Substring Search
Dicription Little Daniel loves to play with strings! He always finds different ways to have fun with ...
随机推荐
- Spring使用外部属性文件
一.在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息.而这些部署细节实际上需要在配置文件外部来定义. 二 ...
- Visual Studio 2012 出现关于ActivityLog.xml错误的解决方案
由sp1升级sp2后出现的错误. devenv.exe /safemode启动下,就可以了 命令列參數 描述 /Command (devenv.exe) 啟動 IDE 並執行指定的命令. /Debug ...
- qt客户端程序使用svg图片资源的几种方法
直接使用svg格式文件资源的情况 1. 直接在UI控件属性面板中选择部分支持icon图标的控件的icon来源,这样图标可以显示 2.给toolbutton添加样式 qproperty-icon: ur ...
- 使用logstash结合logback收集微服务日志
因为公司开发环境没有装elk,所以每次查看各个微服务的日志只能使用如下命令 这样子访问日志是并不方便,于是想为每个微服务的日志都用logstash收集到一个文件out中,那以后只要输出这个文件则可查看 ...
- 微信小程序中的图表构建
第一 html中的代码 <view class="container"> <canvas canvas-id="lineCanvas" bin ...
- Go语言实现:【剑指offer】删除链表中重复的结点
该题目来源于牛客网<剑指offer>专题. 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有重复出现的数字. 示例 1: 输入: 1->2->3->3- ...
- create-react-app 打包后静态文件过大 webpack优化
在最近的项目里,页面和静态文件并不是很多的情况下,打包后发现产出的静态资源却很大. 1.关掉sourcemap 在config/webpack.config.js文件里,大概30几行的位置添加这样一句 ...
- vue路由核心要点(vue-router)
目录 目录 1.vue-router 是什么? 2.如何使用v-router? 3.vue-router跳转和传参 4.vue-router实现的原理 两种模式 5.vue-router 有哪几种导航 ...
- 最近很火的namebase羊毛, 手把手教你怎么薅
闲话少说直接说步骤: 1. 羊毛 https://www.namebase.io/airdrop 要求条件: 1) 要有github账号 2) 2019年2月之前有16+个follower 3) 要有 ...
- pytorch之 batch_train
import torch import torch.utils.data as Data torch.manual_seed(1) # reproducible BATCH_SIZE = 5 # BA ...