题目大意:

给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. 前台Ajax发送数据给后台

    前台发ajax请求给后台 前台代码 let data= [{receiveAdd:receiveAddVal, sendAdd:sendAddVal,distance:distance,goodsNa ...

  2. Java网络编程之TCP、UDP

    Java网络编程之TCP.UDP 2014-11-25 15:23 513人阅读 评论(0) 收藏 举报 分类: java基础及多线程(28) 版权声明:本文为博主原创文章,未经博主允许不得转载.   ...

  3. 多校连萌15-8-12#A

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  4. [Angular] Setup automated deployment with Angular, Travis and Firebase

    Automate all the things!! Automation is crucial for increasing the quality and productivity. In this ...

  5. spring与memcache的整合

    1. pom.xml文件增加: <dependency> <groupId>com.whalin</groupId> <artifactId>Memca ...

  6. vue指令应用--实现输入框常见过滤功能

    前端开发最常碰到的就是输入框,经常要做各种验证,本公司惯用的需求是直接屏蔽特定字符的输入,如禁止非数字输入,特殊符号输入,空格输入等,这些功能反复使用,做成指令的形式,直接调用,非常方便,上代码: 目 ...

  7. 8.4 Android灯光系统_源码分析_电池灯

    电池灯的Java代码在batteryservice.java中 电池的状态电量等信息由驱动获得,但驱动不会主动做这些事情,因此肯定有个App调用驱动程序读取电池信息,称这个App为A应用. 还有个Ap ...

  8. like小计

    1.有索引的列最好进行 ‘aa%’形式可以使用一些索引. 2.如果非得进行 ‘%aa%’这种类型查询,那这个条件不要进行主要过滤条件. 意思是这个列如果有索引就不能用索引,即使用了,索引页是进行对整个 ...

  9. C#判断操作系统类型

    操作系统  PlatformID  主版本号  副版本号  Windows95  1  4  0  Windows98  1  4  10  WindowsMe  1  4  90  WindowsN ...

  10. CImage将图片转为指定像素大小

    CFileDialog fDlg(true, "jpg", "",   OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,   &q ...