题目大意:

给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. Windows下编译OpenSSL(使用VS2013)

    简述 OpenSSL是一个开源的第三方库,它实现了SSL(Secure SocketLayer)和TLS(Transport Layer Security)协议,被广泛企业应用所采用.对于一般的开发人 ...

  2. UVA 11584 - Partitioning by Palindromes DP

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. PythonOOP面向对象编程2

    编程语言的特征: 继承 封装 多态 如:C++ / Java / Python / Swift / C# inheritance 继承 drived 派生 概念: 继承是指从已有的类中衍生出新类,新类 ...

  4. js用button激活 Alert 元素关闭按钮的交互功能

    js用button激活 Alert 元素关闭按钮的交互功能 一.总结 1.点(.)对应class,井号(#)对应id  2.jquery:amaze里面用的jquery,jquery熟悉之后,这些东西 ...

  5. opencv cvtColor dtype issue(error: (-215) )

    opencv cvtColor dtype issue(error: (-215) ) 更详细的错误信息如下,color.cpp:9710: error: (-215) depth == CV_8U ...

  6. 11995 - I Can Guess the Data

    大意:猜数据结构是栈.队列或者优先队列,可能为两种以上,也可能都不是. 水题.. STL 记得判断是否为空 #include<iostream> #include<cstdio> ...

  7. (转)curl 命令使用

    原文地址:http://blog.sina.com.cn/s/blog_4b9eab320100slyw.html 可以看作命令行浏览器 1.开启gzip请求curl -I http://www.si ...

  8. Win10系统如何设置所有程序默认以管理员身份运行?

    原文:Win10系统如何设置所有程序默认以管理员身份运行? 在win10系统中有些用户发现一些程序只有使用管理员身份运行能才打开,这样的话就感觉会麻烦很多,那么有没有办法设置所有程序都默认以管理员身份 ...

  9. swift开发网络篇 - 用户登录POST JSON and header

    版权声明:本文为博主原创文章,未经博主允许不得转载. import UIKit import Alamofire class ViewController: UIViewController { va ...

  10. python画最最简单的折线图

    # encoding=utf-8import matplotlib.pyplot as pltfrom pylab import * #支持中文mpl.rcParams['font.sans-seri ...