HDU 4641
动态更新后缀自动机,每次不断依据当前添加的节点不断往前寻找父节点上字符串最多可出现的次数
这里为了减少运算,当父节点已经达到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的更多相关文章
- hdu 4641 K-string SAM的O(n^2)算法 以及 SAM+并查集优化
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4641 题意:有一个长度为n(n < 5e4)的字符串,Q(Q<=2e5)次操作:操作分为:在末 ...
- HDU 4641 K-string 后缀自动机 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=4641 https://blog.csdn.net/asdfgh0308/article/details/4096 ...
- 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 ...
- K-string HDU - 4641 (后缀自动机)
K-string \[ Time Limit: 2000 ms\quad Memory Limit: 131072 kB \] 题意 给出长度为 \(n\) 的字符串,接下来跟着 \(m\) 次操作, ...
- 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形成的不 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- Sql Server RowNumber和表变量分页性能优化小计
直接让代码了,对比看看就了解了 当然,这种情况比较适合提取字段较多的情况,要酌情而定 性能较差的: WITH #temp AS ( ...
- systemtap执行过程中报probe timer.profile registration error
probe timer.profile registration error 今天在执行火焰图的过程中,代码报错,probe timer.profile registration error 经过查询 ...
- SQLite - SELECT查询
SQLite - SELECT查询 SQLite SELECT语句用于获取数据从一个SQLite数据库表返回数据结果表的形式.也称为result-sets这些结果表. 语法 SQLite SELECT ...
- (转)使用Spring配置文件实现事务管理
http://blog.csdn.net/yerenyuan_pku/article/details/52886207 前面我们讲解了使用Spring注解方式来管理事务,现在我们就来学习使用Sprin ...
- Python 中print 和return 的区别
1.print() print()函数的作用是输出数据到控制台,就是打印在你能看到的界面上. 2.return return语句[表达式]退出函数,选择性地向调用方返回一个表达式.不带参数值的retu ...
- Qt中常用的类
QApplication 应用程序类 管理图形用户界面应用程序的控制流和主要设置 QLabel 标签类 提供 ...
- Kubernetes 架构(下)【转】
上一节我们讨论了 Kubernetes 架构 Master 上运行的服务,本节讨论 Node 节点. Node 是 Pod 运行的地方,Kubernetes 支持 Docker.rkt 等容器 Run ...
- SQL Server数据库的除法默认向下取整,要返回小数的解决方法
num1; / 1000.0 num2; * 1.0 num3; num4; 结果:
- html自动刷新
头部<meta http-equiv="refresh" content="10"> 或者js实现 <script language=&quo ...
- 剑指Offer(Python)
014-链表中倒数第k个结点 用快慢指针:p2比p1先走到k:间隔了k-1)步,然后再一起走,当p2为最后一个时,p1就为倒数第k个数 class ListNode: def __init__(sel ...