好久没有更blog了啊。。。

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

这是一个SAM的模板题。

我好弱啊这个时候才开始学SAM,才会用指针。

要维护3个东西:每个状态right集合的大小、每个状态能到达的所有状态的right集合总大小、每个状态能到达的所有状态数

我没有判-1的情况反正没有-1的数据也能过

//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
#define ll long long
const int maxn=5e5+10;
int n,k,o;
char s[maxn]; char cc;ll ff;
template<typename T>void read(T& aa) {
aa=0;ff=1; cc=getchar();
while(cc!='-'&&(cc<'0'||cc>'9')) cc=getchar();
if(cc=='-') ff=-1,cc=getchar();
while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
aa*=ff;
} struct Sam{
Sam *next[27],*par;
int step;ll right,sum[2];
}pool[2*maxn],*last,*root;
int tot; Sam* newnode(int step) {
Sam* t=pool+(tot++);
memset(t->next,0,sizeof(t->next));
t->par=NULL;
t->step=step;
t->right=t->sum[0]=t->sum[1]=0;
return t;
} void Extend(int w) {
Sam *p=last;
Sam *np=newnode(p->step+1);np->right=1;
for(;p&&!p->next[w];p=p->par) p->next[w]=np;
if(!p) np->par=root;
else {
Sam *q=p->next[w];
if(q->step==p->step+1) np->par=q;
else {
Sam *nq=newnode(p->step+1);
memcpy(nq->next,q->next,sizeof(q->next));
nq->par=q->par;
q->par=nq; np->par=nq;
for(;p&&p->next[w]==q;p=p->par) p->next[w]=nq;
}
}
last=np;
} int c[2*maxn];Sam *sa[2*maxn];
void get_jp() {
Sam *r,*t;
for(r=pool+1;r!=pool+tot;++r) c[r->step]++;
for(int i=1;i<=n;++i) c[i]+=c[i-1];
for(r=pool+1;r!=pool+tot;++r) sa[c[r->step]--]=r;
for(int i=tot-1;i;--i) {
r=sa[i];
r->sum[0]=1;
r->sum[1]=r->right;
t=r->par;
t->right+=r->right;
for(int j=0;j<26;++j) {
t=r->next[j];
if(!t) continue;
r->sum[0]+=t->sum[0];
r->sum[1]+=t->sum[1];
}
}
} void travel() {
Sam *r=root,*t;
while(k) {
for(int i=0;i<26&&k;++i) {
t=r->next[i];
if(!t) continue;
if(t->sum[o]>=k) {
printf("%c",i+'a');
break;
}
k-=t->sum[o];
}
r=t;
if(o==1&&k<=r->right) break;
if(o==0&&k<=1) break;
k-= o==0? 1 : r->right;
}
} int main() {
scanf("%s",s+1); n=strlen(s+1);
last=root=newnode(0);
for(int i=1;i<=n;++i) Extend(s[i]-'a');
get_jp();
read(o); read(k);
travel();
return 0;
}

  

顺便再放一道水题(spoj8222)的代码

//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
#define ll long long
const int maxn=250000+7;
int n,ans[maxn];
char s[maxn]; char cc;ll ff;
template<typename T>void read(T& aa) {
aa=0;ff=1; cc=getchar();
while(cc!='-'&&(cc<'0'||cc>'9')) cc=getchar();
if(cc=='-') ff=-1,cc=getchar();
while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
aa*=ff;
} struct Sam{
Sam *next[27],*par;
int step,right;
}pool[2*maxn],*last,*root;
int tot; Sam* newnode(int step) {
Sam *t=pool+(tot++);
memset(t->next,0,sizeof(t->next));
t->step=step; t->right=0;
return t;
} void Extend(int w) {
Sam *p=last;
Sam *np=newnode(p->step+1); np->right=1;
for(;p&&!p->next[w];p=p->par) p->next[w]=np;
if(!p) np->par=root;
else {
Sam *q=p->next[w];
if(q->step==p->step+1) np->par=q;
else {
Sam *nq=newnode(p->step+1);
memcpy(nq->next,q->next,sizeof(q->next));
nq->par=q->par;
q->par=nq; np->par=nq;
for(;p&&p->next[w]==q;p=p->par) p->next[w]=nq;
}
}
last=np;
} int c[2*maxn];Sam *sa[2*maxn];
void csort() {
Sam *t;
for(t=pool+1;t!=pool+tot;++t) c[t->step]++;
for(int i=1;i<=n;++i) c[i]+=c[i-1];
for(t=pool+1;t!=pool+tot;++t) sa[c[t->step]--]=t;
for(int i=tot-1;i;--i) {
t=sa[i];
ans[t->step]=max(ans[t->step],t->right);
t->par->right+=t->right;
}
} int main() {
scanf("%s",s+1); last=root=newnode(0);
n=strlen(s+1);
for(int i=1;i<=n;++i) Extend(s[i]-'a');
csort();
for(int i=n;i;--i) ans[i]=max(ans[i],ans[i+1]);
for(int i=1;i<=n;++i) printf("%d\n",ans[i]);
return 0;
}

  

bzoj3899 弦论的更多相关文章

  1. 【BZOJ-3998】弦论 后缀自动机

    3998: [TJOI2015]弦论 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2018  Solved: 662[Submit][Status] ...

  2. BZOJ 3998: [TJOI2015]弦论 [后缀自动机 DP]

    3998: [TJOI2015]弦论 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2152  Solved: 716[Submit][Status] ...

  3. 【BZOJ3998】弦论(后缀自动机)

    [BZOJ3998]弦论(后缀自动机) 题面 BZOJ 题解 这题应该很简单 构建出\(SAM\)后 求出每个点往后还能构建出几个串 按照拓扑序\(dp\)一些就好了 然后就是第\(k\)大,随便搞一 ...

  4. Luogu P3975 [TJOI2015]弦论

    题目链接 \(Click\) \(Here\) 题目大意: 重复子串不算的第\(k\)大子串 重复子串计入的第\(k\)大子串 写法:后缀自动机. 和\(OI\) \(Wiki\)上介绍的写法不太一样 ...

  5. BZOJ3899 仙人掌树的同构(圆方树+哈希)

    考虑建出圆方树.显然只有同一个点相连的某些子树同构会产生贡献.以重心为根后(若有两个任取一个即可),就只需要处理子树内部了. 如果子树的根是圆点,其相连的同构子树可以任意交换,方案数乘上同构子树数量的 ...

  6. LG3975 [TJOI2015]弦论

    题意 题目描述 为了提高智商,ZJY开始学习弦论.这一天,她在< String theory>中看到了这样一道问题:对于一个给定的长度为n的字符串,求出它的第k小子串是什么.你能帮帮她吗? ...

  7. 洛谷 P3975 [TJOI2015]弦论 解题报告

    P3975 [TJOI2015]弦论 题目描述 为了提高智商,ZJY开始学习弦论.这一天,她在<String theory>中看到了这样一道问题:对于一个给定的长度为\(n\)的字符串,求 ...

  8. 【BZOJ 3998】 3998: [TJOI2015]弦论 (SAM )

    3998: [TJOI2015]弦论 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2627  Solved: 881 Description 对于一 ...

  9. 洛谷 P3975 / loj 2102 [TJOI2015] 弦论 题解【后缀自动机】【拓扑排序】

    后缀自动机入门. 题目描述 为了提高智商,ZJY 开始学习弦论. 这一天,她在<String theory>中看到了这样一道问题:对于一个给定的长度为 \(n\) 的字符串,求出它的第 \ ...

随机推荐

  1. Windows API GetShortPathName GetLongPathName

    函数原型: The GetShortPathName function retrieves the short path form of a specified input path. DWORD G ...

  2. 如何通过EditPlus远程连接Linux

    1. File - FTP - FTP Settings 2. Add 3. 填写Linux的ip地址及用户名和密码 4. OK

  3. 性能压测中的SLA,你知道吗?

    本文是<Performance Test Together>(简称PTT)系列专题分享的第6期,该专题将从性能压测的设计.实现.执行.监控.问题定位和分析.应用场景等多个纬度对性能压测的全 ...

  4. day19 作业

    今日作业 1.什么是对象?什么是类? 对象:特征和技能的结合体 类:一系列对象 相同的特征和技能的结合体 2.绑定方法的有什么特点 对象调用类内部的函数 称之为绑定方法,特点: 不同的对象调用该绑定方 ...

  5. Vue 将本地图片上传到阿里云

    一.获取服务器通行证(即获取AccessKey和accessKeySecret) getAccess () { let that = this let url = '服务器地址' let params ...

  6. Springboot 之 启动报错-Cannot determine embedded database driver class for database type NONE

    Springboot 之 启动报错-数据库 springboot项目在启动时,报如下错误: Error starting ApplicationContext. To display the auto ...

  7. MFC 双缓存绘图

    在SDI应用程序中,当我们需要时刻动态刷新界面的时候,如果我们一直使用,UpdateAllView()那么就会出现屏幕不停闪烁.闪屏非常严重,特别是一直在动态刷新的时候.并且在闪屏的过程中 我们根本就 ...

  8. JQuery学习:事件绑定&入口函数&样式控制

    1.基础语法学习: 1.事件绑定 2.入口函数 3.样式控制 <!DOCTYPE html> <html lang="en"> <head> & ...

  9. Redis源码解析:20sentinel(一)初始化、建链

    sentinel(哨兵)是redis的高可用解决方案.由一个或多个sentinel实例组成的分布式系统,可以监控任意多个主节点,以及它们属下的所有从节点.当某个主节点下线时,sentinel可以将下线 ...

  10. 【python之路面向对象】初级篇

    概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程最易被初学 ...