NOIP模拟 string - ac自动机
题目大意:
给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自动机的更多相关文章
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- 【XSY3320】string AC自动机 哈希 点分治
题目大意 给一棵树,每条边上有一个字符,求有多少对 \((x,y)(x<y)\),满足 \(x\) 到 \(y\) 路径上的边上的字符按顺序组成的字符串为回文串. \(1\leq n\leq 5 ...
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)
/** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...
- HDU 6096 String (AC自动机)
题意:给出n个字符串和q个询问,每次询问给出两个串 p 和 s .要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量. 析:真是觉得没有思路啊,看了官方题解,真是好复杂. 假设原 ...
- 2017多校第6场 HDU 6096 String AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
- ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠
题目链接:https://vjudge.net/problem/ZOJ-3228 Searching the String Time Limit: 7 Seconds Memory Limi ...
- ZOJ3228 - Searching the String(AC自动机)
题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...
- 2018.08.22 NOIP模拟 string(模拟)
string [描述] 给定两个字符串 s,t,其中 s 只包含小写字母以及*,t 只包含小写字母. 你可以进行任意多次操作,每次选择 s 中的一个*,将它修改为任意多个(可以是 0 个)它的前一个字 ...
随机推荐
- 关于React中,map出来的元素添加事件问题
用es6 map 的写法 直接绑定一个onTouchStart 事件不会报错. 用es5的map写法 如果不加上this 会报这个错误 无法读取未定义的属性 解决的方法是 绑定this 就可以了
- Project Euler 613 Pythagorean Ant(概率+积分)
题目链接:点击我打开题目链接 题目大意: 给你一只蚂蚁,它在一个 边长为 \(30-40-50\) 的直角三角形\((x,y)\)上,并且它在直角三角形中选择的位置和移动方向的概率都是相等的.问你这只 ...
- liunx基本操作常用命令
liunx通常用作服务器,运行服务器软件,服务器要等待,类似超市学关键命令操作 内核,外壳 shell命令跟内核打交道用的是发行版本,不是内核,Radhat公司的CentOS,阿里巴巴也用这个 liu ...
- 微服务实战(二):使用API Gateway - DockOne.io
原文:微服务实战(二):使用API Gateway - DockOne.io [编者的话]本系列的第一篇介绍了微服务架构模式.它讨论了采用微服务的优点和缺点,除了一些复杂的微服务,这种模式还是复杂应用 ...
- php课程 10-34 目录遍历中的注意事项是什么
php课程 10-34 目录遍历中的注意事项是什么 一.总结 一句话总结:用scandir,会把目录和文件放到一个数组中. 1.移动文件怎么实现,php里面没有移动文件这个函数? 先复制,再删除 2 ...
- amazeui学习笔记--js插件(UI增强2)--按钮交互Button
amazeui学习笔记--js插件(UI增强2)--按钮交互Button 一.总结 1.按钮loading状态: <button type="button" class=&q ...
- Android JavaMail介绍及发送一封简单邮件
本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,转载请注明. JavaMail是SUN提供给开 ...
- mysql :Native table 'performance_schema'.'cond_instances' has the wrong structure
err: 150418 13:25:06 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong struct ...
- ITFriend开发日志20140611
原文链接:http://www.itfriend.cn/user/ITFriend/article/details/100274 1.调整登录页. 把大背景图,改为通用的banner图,节省流量. 登 ...
- js和jquery实现页面滚动监听
js和jquery实现页面滚动监听 一.总结 一句话总结:onscroll方法和监听页面元素的高度都可以实现滚动监听. 1.onscroll方法实现滚动监听的核心代码是什么? <body ons ...