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 ...
随机推荐
- [已读]HTML5与CSS3设计模式
我想说,不要被书名骗了,其实并没有涉及丁点h5与css3的内容,但是确实称得上比较详细的一本关于css的书.看它的页数就知道了,481~~ 今年上半年看完的,现在想想,觉得自己还是蛮拼的.内容会比较枯 ...
- 用户名密码登录小程序及input与raw_input区别。
一.此次程序需要实现: 1.设定固定的用户名密码 2.用户名密码输入正确打印登录正确信息 3.仅仅运行三次登录 二.本次使用的python版本为: Windows下版本号: C:\Users\dais ...
- Apache Cordova
http://cordova.apache.org/ Apache Cordova is a platformfor building native mobile applications using ...
- hihocoder1779 公路收费
思路: 枚举每个点做根即可. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; const l ...
- AlertDialog的几种用法
xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...
- SVN与TFS自动同步脚本(很实用)
一直都在园子里看文章,因为各种原因懒得写文章.最近稍得空闲,把这几天的工作成果分享一下. 因为工作需要,开发人员使用Qt进行系统移动端的开发,Qt的版本控制却不提供连接TFS的设置,只有使用svn.没 ...
- UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...
- 【前端】pid2children iview tree json
<script> import inBody from '../inBody' export default { components:{ inBody } ,data () { retu ...
- stay hungry stay foolish.
I am honored to be with you today at your commencement from one of the finest universities in the wo ...
- 最短路 || HDU 2066 一个人的旅行
本草的旅行故事(✺ω✺),可以从S个点中的任意一个开始,到达D个点中的任意一个,求最短路 *解法:把草儿的家记成点0,S个点与0的距离为0,然后spfa求最短路 又是改了一万次,①多组数据啊 ②改完多 ...