动态更新后缀自动机,每次不断依据当前添加的节点不断往前寻找父节点上字符串最多可出现的次数

这里为了减少运算,当父节点已经达到k次就不在往前寻找,因为之前的必然达到k次,也已经统计在内

 #include <cstdio>
#include <cstring>
#include <iostream> using namespace std;
#define N 500010
#define M 26 struct SamNode{
SamNode *son[] , *f;
int l , sc;
void init(){
f = NULL;
for(int i= ; i< ; i++) son[i] = NULL;
l = sc = ;
}
}*root , *last , sam[N]; int cnt , n , m , k , ret;
char s[N]; void init(){
sam[].init();
root = last = &sam[cnt=];
ret = ;
} void add(int x)
{
SamNode *p = &sam[++cnt] , *jp=last;
p->init();
p->l = jp->l+;
last = p;
for( ; jp&&!jp->son[x] ; jp=jp->f) jp->son[x]=p;
if(!jp) p->f = root;
else{
if(jp->l+ == jp->son[x]->l) p->f = jp->son[x];
else{
SamNode *r = &sam[++cnt] , *q = jp->son[x];
r->init();
*r = *q;
r->l = jp->l+;
q->f = p->f = r;
for( ; jp && jp->son[x]==q ; jp=jp->f) jp->son[x]=r;
}
}
while(p!=root && p->sc<k){
p->sc++;
if(p->sc == k) ret+=(p->l-p->f->l);
p = p->f;
}
} void solve()
{
for(int i= ; i<n ; i++) add(s[i]-'a');
// for(int i=0 ; i<=5 ; i++) cout<<i<<" :"<<sam[i].sc<<endl;
int op;
char c[];
for(int i= ; i<m ; i++){
scanf("%d" , &op);
if(op == ) printf("%d\n" , ret);
else{
scanf("%s" , c);
add(c[]-'a');
}
//debug
// for(int i=0 ; i<=5 ; i++) cout<<i<<" :"<<sam[i].sc<<endl;
}
} int main()
{
// freopen("a.in" , "r" , stdin);
while(~scanf("%d%d%d" , &n , &m , &k))
{
scanf("%s" , s);
init();
solve();
}
return ;
}

HDU 4641的更多相关文章

  1. hdu 4641 K-string SAM的O(n^2)算法 以及 SAM+并查集优化

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4641 题意:有一个长度为n(n < 5e4)的字符串,Q(Q<=2e5)次操作:操作分为:在末 ...

  2. HDU 4641 K-string 后缀自动机 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=4641 https://blog.csdn.net/asdfgh0308/article/details/4096 ...

  3. HDU 4641 K-string

    Description Given a string S. K-string is the sub-string of S and it appear in the S at least K time ...

  4. K-string HDU - 4641 (后缀自动机)

    K-string \[ Time Limit: 2000 ms\quad Memory Limit: 131072 kB \] 题意 给出长度为 \(n\) 的字符串,接下来跟着 \(m\) 次操作, ...

  5. hdu 5343 MZL's Circle Zhou SAM

    MZL's Circle Zhou 题意:给定两个长度不超过a,b(1 <= |a|,|b| <= 90000),x为a的连续子串,b为y的连续子串(x和y均可以是空串):问x+y形成的不 ...

  6. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  8. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. 【前端】模拟微信上传图片(带预览,支持预览gif)

    一.Html <style type="text/css"> #previewDiv{width:50px;height:50px;overflow:hidden;po ...

  2. List的深度copy和浅度拷贝

    List<Student> list= Arrays.asList( new Student("Fndroid", 22, Student.Sax.MALE, 180) ...

  3. 修改他人电脑的Windows登录密码

    在别人电脑已登录Windows的情况下: 打开控制面板 -> 管理工具 -> 计算机管理   或者  对Win图标单击右键 -> 计算机管理 -> 本地用户和组 -> 用 ...

  4. 02html基础

    02_html 1.几个标签 1.1 meta标签 meta标签的属性有name和http-equiv,其中name属性用于描述网页,对应于content(网页内容). <meta name=& ...

  5. redis源码分析之事务Transaction(下)

    接着上一篇,这篇文章分析一下redis事务操作中multi,exec,discard三个核心命令. 原文地址:http://www.jianshu.com/p/e22615586595 看本篇文章前需 ...

  6. C++ isalpha、isalnum、islower、isupper用法

    1. isalpha isalpha()用来判断一个字符是否为字母,如果是字符则返回非零,否则返回零. cout<<isalpha('a'); //返回非零 cout<<isa ...

  7. postman做压力测试

    压力测试 当你需要验证你的接口的抗压能力的时候,可以点击Runner,进行压力测试 注意:压力测试只能以文件夹的方式执行多个接口,不能单独执行,如果想要测试某一个接口,就创一个文件夹,这个文件夹里只有 ...

  8. Windows下使用ffmpeg与java实现截取视频缩略图

    [ffmpeg.exe可执行文件获取]: 网上搜索后得到编译好的ffmpeg文件,下载地址:http://download.csdn.net/source/453719 [安装]: 将下载的文件解压, ...

  9. PHP06 流程控制

    学习要点 选择结构 循环结构 学习目标 掌握PHP的选择结构 掌握PHP的循环结构 流程控制概述 程序 程序:一系列计算机指令的集合. 编程语言:开发程序的工具. 程序执行结构 计算机程序有三种基本执 ...

  10. CentOS6.8下安装Docker

    原文章链接https://www.cnblogs.com/baolong/p/5743420.html. 由于在自己安装的虚拟机上打开linux终端命令行输入uname -a 以及cat /etc/r ...