洛谷 SP7258 SUBLEX - Lexicographical Substring Search

Problem

先给你一个字符串s,后有T次询问。询问这个字符串的所有本质不同的子串中第k小的子串。

\(\mid s\mid\le9\times 10^4,T\le500\)

Solution

这也是一道SAM的模板题。

字典序第k小子串对应SAM中字典序第k小的路径。用拓扑排序计算每个状态的路径数之后,我们从SAM的根节点开始跑,类比平衡树等数据结构中的查询第k小的数这一个操作,很容易就可以找到第k小的路径。

Code

/**************************************************************
* Problem: SP7258
* Author: Vanilla_chan
* Date: 20210325
* E-Mail: Vanilla_chan@outlook.com
**************************************************************/
//预编译部分已略
namespace SAM
{
const int maxn=90010;
struct state
{
int len,link;
LL f;
map<char,int>nxt;
state operator=(const state& z)
{
len=z.len;
link=z.link;
nxt=z.nxt;
f=0;
return *this;
}
}st[maxn<<1];
int cnt=1,last=1;
void insert(const char& ch)
{
int p=last;
int cur=last=++cnt;
st[cur].len=st[p].len+1;
st[cur].f=1;
while(p&&!st[p].nxt.count(ch))
{
st[p].nxt[ch]=cur;
p=st[p].link;
}
if(!p)
{
st[cur].link=1;
}
else
{
int q=st[p].nxt[ch];
if(st[p].len+1==st[q].len)//
{
st[cur].link=q;
}
else
{
int nq=++cnt;
st[nq]=st[q];
st[nq].len=st[p].len+1;//
st[q].link=st[cur].link=nq;
while(p&&st[p].nxt[ch]==q)
{
st[p].nxt[ch]=nq;
p=st[p].link;
}
}
}
}
};
#define N 90010
LL ans;
int in[N<<1];
int l,r;
int q[N<<1];
int sze[N<<1];
void top()
{
using namespace SAM;
for(int i=1;i<=cnt;i++)
{
for(map<char,int>::iterator it=st[i].nxt.begin();it!=st[i].nxt.end();it++)
{
in[it->second]++;
}
}
l=1,r=0;
for(int i=1;i<=cnt;i++) if(in[i]==0) q[++r]=i;
for(;l<=r;l++)
{
for(map<char,int>::iterator it=st[q[l]].nxt.begin();it!=st[q[l]].nxt.end();it++)
{
in[it->second]--;
if(in[it->second]==0)
{
q[++r]=it->second;
}
}
}
for(int i=r;i>=1;i--)
{
sze[q[i]]=1;
for(map<char,int>::iterator it=st[q[i]].nxt.begin();it!=st[q[i]].nxt.end();it++)
{
sze[q[i]]+=sze[it->second];
}
}
}
void ask(int x,int k)
{
using namespace SAM;
while(k)
{
for(map<char,int>::iterator it=st[x].nxt.begin();it!=st[x].nxt.end();it++)
{
if(sze[it->second]>=k)
{
putchar(it->first);
x=it->second;
k--;
break;
}
else k-=sze[it->second];
}
}
cout<<endl;
}
string str;
int n,t,k;
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
oi::cin>>str;
for(int i=0;i<str.size();i++)
{
SAM::insert(str[i]);
}
top();
t=read();
while(t--)
{
oi::cin>>k;
ask(1,k);
}
return 0;
}

extend

注意这道题是求本质不同的,即,对于本质相同、不同位置子串,看作是一种。

接下来不妨去做一下洛谷 P3975 [TJOI2015]弦论,求对于所有子串的所有子串(不同位置的相同子串算作多个)的第k小子串。

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

  1. SP7258 SUBLEX - Lexicographical Substring Search

    \(\color{#0066ff}{ 题目描述 }\) 给定一个字符串,求排名第k小的串 \(\color{#0066ff}{输入格式}\) 第一行给定主串(len<=90000) 第二行给定询 ...

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

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

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

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

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

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

  5. SPOJ7258 SUBLEX - Lexicographical Substring Search

    传送门[洛谷] 心态崩了我有妹子 靠 我写的记忆化搜索 莫名WA了 然后心态崩了 当我正要改成bfs排序的时候 我灵光一动 md我写的i=0;i<25;i++??? 然后 改过来就A掉了T^T ...

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

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

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

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

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

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

  9. SPOJ:SUBLEX - Lexicographical Substring Search

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

  10. Spoj SUBLEX - Lexicographical Substring Search

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

随机推荐

  1. 【数值计算方法】线性方程组迭代算法的Python实现

    线性方程组迭代算法的Python实现 jacobi,GS,SOR迭代法 def JacobiIter(A:np.ndarray, b:np.ndarray, tol:float=1e-5, maxIt ...

  2. Java的volatile和sychronized底层实现

    1. 概览 从Java代码级别到硬件级别各层都是如何实现的 2. Synchronized 2.1 字节码层面 使用javap -verbose <class文件>可以查看到字节码信息,其 ...

  3. 容器引擎-Docker

    Docker是一个开源的应用容器引擎,可以轻松的为任何应用创建一个轻量级.可移植的.自给自足的容器.Docker类似于集装箱,各式各样的货物,经过集装箱的标准化进行托管,而集装箱和集装箱之间没有影响. ...

  4. SeaBIOS实现简单分析

    SeaBIOS是一个16bit的x86 BIOS的开源实现,常用于QEMU等仿真器中使用.本文将结合SeaBIOS Execution and code flow和SeaBIOS的源码对SeaBIOS ...

  5. Delphi字符串加密解密函数

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. Browser-use 详细介绍&使用文档

    Browser-use 详细介绍&使用文档 一.概述 Browser-use 是一个旨在将 AI "智能体"(Agents)与真实浏览器进行交互的 Python 库,可以轻 ...

  7. nacos(九):sentinel——规则持久化

    接上回,sentinel基本使用我们已经掌握.但是在设置限流规则时,会发现规则都是临时的,一段时间没访问资源或者重启sentinel,规则就会消失.所以,我们需要有一个将规则持久化保存的地方,让规则一 ...

  8. IDEA中高效配置Python开发环境搭建

    原文地址:IDEA中高效配置Python开发环境搭建-张苹果博客 Mac用户须知:系统已预装Python 2.7,如需新版建议通过Homebrew安装. # 张苹果博客:https://zhangpi ...

  9. SMU Autumn 2024 Trial 1

    A. Load Balancing 很明显题意要的就是让我们把每个数往平均值靠,这样就保证最大值-最小值最小 但是当sum%n !=0的时候就说明无法每个数都等于sum/n,所以处理的方法就是,先计算 ...

  10. SpringSecurity5(14-Gateway整合)

    MVC 与 WebFlux 关系 SpringSecurity 设置要采用响应式配置,基于 WebFlux 中 WebFilter 实现,与 Spring MVC 的 Security 是通过 Ser ...