题目大意:

给n个字符串(100位以内),和一个长串s(100000位以内),求n个字符串在s中出现的次数。然后给出m次修改,每次修改s中的一个字符,对于每次修改,输出更新后的答案(修改会保存)。

题目分析:

没有修改就是ac自动机裸题。虽然带了修改,但发现匹配的字符串最多才100位,也就是说每次修改的pos位置的字符最多影响[pos-l,pos+l]这个区间的答案,于是便有了正解:每次修改前,将该区间的答案减掉,在加上修改后该区间的答案。

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
using namespace std; struct node{
int trans[30], fail, cnt;
inline void init(){
memset(trans, 0, sizeof trans);
fail = cnt = 0;
}
}tr[1000005];
int tot = 0; inline void insert(string &s){
int pos = 0;
for(int i = 0; i < s.length(); i++){
if(!tr[pos].trans[s[i] - 'a' + 1]) tr[tr[pos].trans[s[i] - 'a' + 1] = ++tot].init();
pos = tr[pos].trans[s[i] - 'a' + 1];
}
tr[pos].cnt++;
} inline void build_fail(){
queue<int> que;
que.push(0);
while(!que.empty()){
int u = que.front(); que.pop();
tr[u].cnt += tr[tr[u].fail].cnt;
for(int i = 1; i <= 26; i++){
if(tr[u].trans[i]){
que.push(tr[u].trans[i]);
if(u) tr[tr[u].trans[i]].fail = tr[tr[u].fail].trans[i];
}
else tr[u].trans[i] = tr[tr[u].fail].trans[i];
}
}
} string t;
inline int ac(int l, int r){
int ans = 0, pos = 0;
for(int i = l; i <= r; i++){
pos = tr[pos].trans[t[i] - 'a' + 1];
ans += tr[pos].cnt;
}
return ans;
} int maxl;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
int n, q; cin >> n >> q;
for(int i = 1; i <= n; i++){
string s;
cin >> s;
insert(s); int len = s.length();
maxl = max(maxl, len);
}
build_fail();
cin >> t;
int ans;
int len = t.length();
cout << (ans = ac(0, len - 1)) << endl;
for(int i = 1; i <= q; i++){
int pos; char c; cin >> pos >> c; pos--; ans -= ac(max(0, pos - maxl), min(len - 1, pos + maxl));
t[pos] = c;
ans += ac(max(0, pos - maxl), min(len - 1, pos + maxl));
cout << ans << endl;
}
return 0;
}

NOIP模拟 string - ac自动机的更多相关文章

  1. ZOJ 3228 Searching the String(AC自动机)

    Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to d ...

  2. 【XSY3320】string AC自动机 哈希 点分治

    题目大意 给一棵树,每条边上有一个字符,求有多少对 \((x,y)(x<y)\),满足 \(x\) 到 \(y\) 路径上的边上的字符按顺序组成的字符串为回文串. \(1\leq n\leq 5 ...

  3. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  4. zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)

    /** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...

  5. HDU 6096 String (AC自动机)

    题意:给出n个字符串和q个询问,每次询问给出两个串 p 和 s .要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量. 析:真是觉得没有思路啊,看了官方题解,真是好复杂. 假设原 ...

  6. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

  7. ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠

    题目链接:https://vjudge.net/problem/ZOJ-3228 Searching the String Time Limit: 7 Seconds      Memory Limi ...

  8. ZOJ3228 - Searching the String(AC自动机)

    题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...

  9. 2018.08.22 NOIP模拟 string(模拟)

    string [描述] 给定两个字符串 s,t,其中 s 只包含小写字母以及*,t 只包含小写字母. 你可以进行任意多次操作,每次选择 s 中的一个*,将它修改为任意多个(可以是 0 个)它的前一个字 ...

随机推荐

  1. 利用formdata对象上传文件时,需要添加的参数

    function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: 'h ...

  2. 【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】

    [199-Binary Tree Right Side View(从右边看二叉树] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 代码下载[https://github.co ...

  3. ASM学习笔记--ASM 4 user guide 第一章翻译

    ASM是什么? 借用别人的话 :ASM 是一个 Java 字节码操控框架.它能被用来动态生成类或者增强既有类的功能. ASM 可以直接产生二进制 class 文件,也可以在类被加载入 Java 虚拟机 ...

  4. Node.js自学笔记之回调函数

    写在前面:如果你是一个前端程序员,你不懂得像PHP.Python或Ruby等动态编程语言,然后你想创建自己的服务,那么Node.js是一个非常好的选择.这段时间对node.js进行了简单的学习,在这里 ...

  5. 【z05】聪明的质检员

    [题目链接]:http://noi.qz5z.com/viewtask.asp?id=z05 [题解] 显然w越大,最后的Y也就越大; 可以依靠这个搞二分: 如果二分枚举的tw得到的Y比S小,则减小t ...

  6. C/C++ 程序的跟踪和分析工具 uftrace

    uftrace 用于跟踪和分析 C/C++ 编写的程序的执行情况,它受到 Linux 内核的 ftrace 框架的启发(特别是 function graph tracer),支持 userspace ...

  7. S​D​I​与​A​S​I 接口具体解释介绍

    分量编码 在对彩色电视信号进行数字化处理和传输是.一种经常使用的方式是分别对其3个分量(Y,R-Y.B-Y)进行数字化编码.这就是分量分量编码.另外还有全信号编码,全信号编码是对彩色全电视信号直接进行 ...

  8. Normal Equation of Computing Parameters Analytically

    Normal Equation Note: [8:00 to 8:44 - The design matrix X (in the bottom right side of the slide) gi ...

  9. Vue源码--解读vue响应式原理

    原文链接:https://geniuspeng.github.io/2018/01/05/vue-reactivity/ Vue的官方说明里有深入响应式原理这一节.在此官方也提到过: 当你把一个普通的 ...

  10. [Docker] Run Short-Lived Docker Containers

    Learn the benefits of running one-off, short-lived Docker containers. Short-Lived containers are use ...