对于一个给定长度为\(N\)的字符串,求它的第\(K\)小子串是什么。

Input

第一行是一个仅由小写英文字母构成的字符串\(S\)

第二行为两个整数\(T\)和\(K\),\(T\)为0则表示不同位置的相同子串算作一个。\(T=1\)则表示不同位置的相同子串算作多个。\(K\)的意义如题所述。

Output

输出仅一行,为一个数字串,为第\(K\)小的子串。如果子串数目不足\(K\)个,则输出\(-1\)

Sample Input

aabc
0 3

Sample Output

aab

Hint

\(N<=5*10^5\)

\(T<2\)

\(K<=1e9\)

题意:

中文题面,不解释。

题解:

把串放进后缀自动机,然后处理一遍,如果\(T=0\),则所有点权为1;否则,把每个点的\(parent\)加上当前\(size\)。然后反向拓扑,像求第\(k\)大子串如这个一样求就行了。

#include<bits/stdc++.h>
using namespace std;
const int N=1000010;
char s[N];
int a[N],c[N];
void cmax(int &a,int b){
a=max(a,b);
}
void cmin(int &a,int b){
a=min(a,b);
}
struct SAM{
int last,cnt;
int size[N],ch[N][26],fa[N<<1],l[N<<1],sum[N];
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=1;
else{
int q=ch[p][c];
if(l[p]+1==l[q])fa[np]=q;
else{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
size[np]=1;
}
void build(char s[]){
int len=strlen(s+1);
last=cnt=1;
for(int i=1;i<=len;++i)ins(s[i]-'a');
}
void calc(int op){
memset(c,0,sizeof c);
for(int i=1;i<=cnt;++i)c[l[i]]++;
for(int i=1;i<=cnt;++i)c[i]+=c[i-1];
for(int i=1;i<=cnt;++i)a[c[l[i]]--]=i;
for(int i=cnt;i;--i){
int p=a[i],f=fa[p];
if(op){
size[f]+=size[p];
}else{
size[p]=1;
}
}
size[1]=0;
for(int i=cnt;i;--i){
int p=a[i];
sum[p]=size[p];
for(int j=0;j<26;++j){
if(ch[p][j])sum[p]+=sum[ch[p][j]];
}
}
}
void find(int k){
int p=1;
size[0]=0;
while(k){
int a=0;
while(k>sum[ch[p][a]]&&a<26){
if (ch[p][a]) k-=sum[ch[p][a]];
a++;
}
if(a>=26){
puts("-1");
return;
}
putchar('a'+a);k-=size[ch[p][a]];
if(k<=0)return;
p=ch[p][a];
}
}
}sam;
int main(){
cin>>s+1;
sam.build(s);
int t,k;
cin>>t>>k;
sam.calc(t);
sam.find(k);
}

弦论(tjoi2015,bzoj3998)(sam(后缀自动机))的更多相关文章

  1. Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))

    Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...

  2. LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  3. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

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

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

  5. Substrings(SPOJ8222) (sam(后缀自动机))

    You are given a string \(S\) which consists of 250000 lowercase latin letters at most. We define \(F ...

  6. sam(后缀自动机)

    后缀自动机ins解释 void ins(int c){ int p=last;//将当前节点的parent节点变为last int np=++cnt;//建立新节点 last=np;//将last设为 ...

  7. Luogu P3346 [ZJOI2015]诸神眷顾的幻想乡 广义SAM 后缀自动机

    题目链接 \(Click\) \(Here\) 真的是好题啊-不过在说做法之前先强调几个自己总是掉的坑点. 更新节点永远记不住往上跳\(p = fa[p]\) 新建节点永远记不住\(len[y] = ...

  8. 字符串(tjoi2016,heoi2016,bzoj4556)(sam(后缀自动机)+线段树合并+倍增+二分答案)

    佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了 一个长为\(n\)的字符串\(s\),和\(m\)个问题.佳媛姐姐必须正确回答这\(m\)个问题, ...

  9. 后缀自动机SAM学习笔记

    前言(2019.1.6) 已经是二周目了呢... 之前还是有一些东西没有理解到位 重新写一下吧 后缀自动机的一些基本概念 参考资料和例子 from hihocoder DZYO神仙翻译的神仙论文 简而 ...

随机推荐

  1. web前端js 实现打印操作

    转载来源:https://www.cnblogs.com/potatog/p/7412905.html 一.打印当前页面指定元素中的内容 方式一:直接使用window.print(); (1)首先获得 ...

  2. Django配置Bootstrap, js

    1.首先在APP目录下创建一个static文件夹 如图: # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'dj ...

  3. 配置 Mysql 支持远程访问 并取消域名解析以提高链接速度

    1 配置远程访问 1.1 修改 my.cnf [mysqld] 段 bind-address = 0.0.0.0 //支持所有 ipv4 1.2 建立远程访问用户 mysql> grant al ...

  4. cmd里面怎么复制粘贴

    不要打开快速编辑模式,他只能复制粘贴cmd里面的内容 其实用标记即可. 右键选择标记,然后框选内容后右键就复制了 然后再右键粘贴就行了.

  5. Json和XML解析

    NSXMLParse 关于XML,有两种解析方式,分别是SAX(Simple API for XML,基于事件驱动的解析方式,逐行解析数据,采用协议回调机制)和DOM(Document Object ...

  6. dbutils封装对象,单列,一行一列(用)

    基本用法:查找并封装对象与对象集合 public User findUserByNamePassword(String name,String password){ QueryRunner runne ...

  7. 2018.08.22 hyc的xor/mex(线段树/01trie)

    hyc的xor/mex 描述 NOIP2017就要来了,备战太累,不如做做hyc的新题? 找回自信吧! 一句话题意:n个数,m个操作 操作具体来讲分两步 1.读入x,把n个数全部xor上x 2.询问当 ...

  8. hdu-1140(求距离,精度判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1140 思路:卫星只能消灭地面上一部分的风暴,即风暴与卫星的距离最大是卫星到地球的切线的距离,大于这个距 ...

  9. 第一章:模型层model layer

    1.模型和字段 (1)基本的原则如下: 每个模型在Django中的存在形式为一个Python类 每个模型都是django.db.models.Model的子类 模型的每个字段(属性)代表数据表的某一列 ...

  10. ESP8266-01一些内容

    系统有些指令必须特性状态下才可以执行,即先执行一些其它AT指令 1.设置station模式的IP信息 AT+CIPSTA="192.168.19.1","192.168. ...