弦论(tjoi2015,bzoj3998)(sam(后缀自动机))
对于一个给定长度为\(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(后缀自动机))的更多相关文章
- Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))
Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...
- 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, \(\ ...
- LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- Lexicographical Substring Search (spoj7259) (sam(后缀自动机)+第k小子串)
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- Substrings(SPOJ8222) (sam(后缀自动机))
You are given a string \(S\) which consists of 250000 lowercase latin letters at most. We define \(F ...
- sam(后缀自动机)
后缀自动机ins解释 void ins(int c){ int p=last;//将当前节点的parent节点变为last int np=++cnt;//建立新节点 last=np;//将last设为 ...
- Luogu P3346 [ZJOI2015]诸神眷顾的幻想乡 广义SAM 后缀自动机
题目链接 \(Click\) \(Here\) 真的是好题啊-不过在说做法之前先强调几个自己总是掉的坑点. 更新节点永远记不住往上跳\(p = fa[p]\) 新建节点永远记不住\(len[y] = ...
- 字符串(tjoi2016,heoi2016,bzoj4556)(sam(后缀自动机)+线段树合并+倍增+二分答案)
佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了 一个长为\(n\)的字符串\(s\),和\(m\)个问题.佳媛姐姐必须正确回答这\(m\)个问题, ...
- 后缀自动机SAM学习笔记
前言(2019.1.6) 已经是二周目了呢... 之前还是有一些东西没有理解到位 重新写一下吧 后缀自动机的一些基本概念 参考资料和例子 from hihocoder DZYO神仙翻译的神仙论文 简而 ...
随机推荐
- Java图形化界面设计——布局管理器之GridBagLayout
GridBagLayout 不会随着窗口的变化标签随之发生变化,可固定. ---------------------------------------------------------- impo ...
- Windows下误删资料的恢复
只要三步,就能找回你删掉并清空回收站的东西 : 1.打开“运行”消息框,然后输入regedit (打开注册表) 2.依次展开:HEKEY——LOCAL——MACHIME/SOFTWARE/micros ...
- Kendo UI中TreeView 放入tabstrip中,大数据量时超过边框的解决方案。
参考http://www.kendoui.com/forums/ui/tabstrip/tabstip-with-treeview-treeview-breaking-out-of-tabstrip. ...
- 二进制搭建kubernetes多master集群【四、配置k8s node】
上一篇我们部署了kubernetes的master集群,参考:二进制搭建kubernetes多master集群[三.配置k8s master及高可用] 本文在以下主机上操作部署k8s node k8s ...
- 2018.07.20 atcoder Largest Smallest Cyclic Shift(贪心)
传送门 题意:给你x个a,y个b,z个c,显然这些字符可以拼成若干字符串,然后求这些字符串中最小表示法表示出来的最大的那一个. 解法:贪心思想,用multiset维护现在拼成的字串,每次取一个最小的和 ...
- 【Unity】1.1 安装Unity 5.3.4 开发环境
分类:Unity.C#.VS2015 创建日期:2016-03-23 一.简介 Unity分个人版(Personal)和专业版(Pro).个人版是免费的(部分高级功能受限,但初学者也用不到它),Pro ...
- 11) 生成可执行jar文件 maven-shade-plugin
搜索 site:maven.apache.org maven-assembly-plugin http://maven.apache.org/plugins/maven-assembly-plugin ...
- NSData与UIImage之间的转换
1 //NSData转换为UIImage 2 NSData *imageData = [NSData dataWithContentsOfFile: imagePath]; 3 UIImage *im ...
- 开源投影工具Proj——进行坐标转换
proj.4 is a standard UNIX filter function which converts geographic longitude and latitude coordinat ...
- 201521123035-个人作业4——alpha阶段个人总结
个人总结 在alpha 结束之后, 每位同学写一篇个人博客, 总结自己的alpha 过程: 第一部分 类别 具体技能和面试问题 现在的回答(大三) 语言 最拿手的计算机语言是一?,代码量多少?(偏前端 ...