好久没有更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;
}

  

转载于:https://www.cnblogs.com/Serene-shixinyi/p/8206900.html

bzoj3899 弦论相关推荐

  1. [BZOJ3998][TJOI2015]弦论

    [BZOJ3998][TJOI2015]弦论 试题描述 对于一个给定长度为N的字符串,求它的第K小子串是什么. 输入 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不 ...

  2. 通俗科普:弦论要求空间必须是25维的解释

    为什么弦论要求空间必须是25维? 因为在25维下,光子质量才会为0.这样才能适用于电磁力.引力. 为什么25维下,光子质量才会为0?看了<超弦理论:探究时间.空间及宇宙的本原>,提到了解释 ...

  3. 洛谷-P3975 弦论(后缀自动机板子题)

    弦论 一道板子题,让我感觉板子题都还不会...然后把递推写成dfs时没有给string加上&,导致内存爆了,2333,果然还是传引用好 题意:求字典序第K小子串以及本质不同的第K小子串 思路: ...

  4. 丘成桐谈几何:从黎曼、爱因斯坦到弦论

    来源 : 超级数学建模 著名数学家丘成桐先生发表了题为"几何:从黎曼.爱因斯坦到弦论"的演讲,追溯了为广义相对论发展奠定基础的的黎曼几何,回顾了影响广义相对论发展的物理学突破,并谈 ...

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

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

  6. [Luogu P3975] [TJOI2015]弦论

    洛谷传送门 BZOJ传送门 题目描述 为了提高智商,ZJY开始学习弦论.这一天,她在< String theory>中看到了这样一道问题:对于一个给定的长度为nnn的字符串,求出它的第k& ...

  7. [bzoj3998]弦论

    [bzoj3998]弦论 建后缀自动机之后初始化每个点和其能到达的点表示的最小和最大的rank后在SAM上跑一遍即可 T=0/1的时候要分类讨论 代码 #include<bits/stdc++. ...

  8. P3975 [TJOI2015]弦论 (SAM)

    [TJOI2015]弦论 - 洛谷 感觉位置不同算相同还是比较好想的,我们top排序后算每个点后面连接了多少点就知道里面有多少字串了,然后求k大就行了: 考了位置不同算不同的话,我们就要考虑endpo ...

  9. P3975 [TJOI2015]弦论

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

最新文章

  1. linux对方开放端口,Linux 开放端口
  2. 区块链太太太太太难了,我花了一分钟才搞懂!
  3. python里的符号区别_Python中的方括号和点符号有什么区别?
  4. linux之安装Clion和运行使用总结
  5. Maven pom.xml 元素配置说明(一)
  6. php 强制刷新一次,强制浏览器使用PHP刷新所有内容
  7. 怎么查linux上谁删了文件,如何在 Linux 下快速找到被删除的文件?
  8. Linux高级权限管理
  9. 最小化安装CentOS 7后,图形界面的安装(GNOME、KDE等)
  10. OAuth2.0学习(1-11)新浪开放平台微博认证-使用OAuth2.0调用微博的开放API
  11. Mysql 报错:Unknown collation: ‘utf8mb4_0900_ai_ci‘
  12. 【AHP】层次分析法 | 过程解读 案例实践
  13. [汇编语言例题]计算地址连续的ffff:0~ffff:b单元中的数据的和(详解)
  14. 博途V16 更改PLC的型号和固件版本
  15. 【华为云技术分享】快速搭建网站之云速建站
  16. mysql 错误 1548_mysql报1548错误-Cannot load from mysql.proc. The table is probably corrupted
  17. 将动画角色导入Unity 3D
  18. [C++] 内联函数inline 以及 auto关键字 -- C++入门(4)
  19. 计算机网络原理与技术
  20. IOS gif图片播放 swift

热门文章

  1. java打开jnlp_jnlp文件怎么打开
  2. 从黑格尔的正反合理论看人生的三个阶段
  3. 产品设计体会(0013)产品经理应该是管理者么
  4. 深度相机Kinect2.0三维点云拼接实验(三)
  5. android限制app运行时间限制,android app 限制app使用时间
  6. 裸辞闹了个乌龙,就当做个题提升自己吧
  7. 常见的国外主机提供商小结
  8. SDL2中文API(整理)
  9. word2vec加载异常解决:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode bytes in position。。。
  10. 【excel】开启了循环引用怎么关闭