[洛谷3808]【模板】AC自动机(简单版)
题目大意:
给定$n$个模式串$p(\sum|p_i|\le10^6)$和一个$t(|t|\le10^6)$,求在$t$中被匹配的$p$的个数。
思路:
AC自动机模板题,注意$t$中一个字符可能对应自动机上多个结点,因此需要按照失配指针跳转统计。统计过的结点需要特殊标记,避免重复统计,否则会超时。
#include<queue>
#include<cstdio>
#include<cctype>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=1e6+,S=;
char s[N];
int ans=;
class AhoCorasick {
private:
std::queue<int> q;
int val[N],ch[N][S],fail[N];
int sz,new_node() {
return ++sz;
}
int idx(const char &c) const {
return c-'a';
}
public:
void insert(const char s[]) {
int p=;
for(register int i=;s[i];i++) {
if(!ch[p][idx(s[i])]) ch[p][idx(s[i])]=new_node();
p=ch[p][idx(s[i])];
}
val[p]++;
}
void get_fail() {
for(register int i=;i<S;i++) {
if(ch[][i]) q.push(ch[][i]);
}
while(!q.empty()) {
const int &x=q.front();
for(register int i=;i<S;i++) {
if(!ch[x][i]) {
ch[x][i]=ch[fail[x]][i];
continue;
}
fail[ch[x][i]]=ch[fail[x]][i];
q.push(ch[x][i]);
}
q.pop();
}
}
void find(const char s[]) {
int p=;
for(register int i=;s[i];i++) {
p=ch[p][idx(s[i])];
for(register int q=p;q&&~val[q];q=fail[q]) {
ans+=val[q];
val[q]=-;
}
}
}
};
AhoCorasick ac;
int main() {
const int n=getint();
for(register int i=;i<n;i++) {
scanf("%s",s);
ac.insert(s);
}
ac.get_fail();
scanf("%s",s);
ac.find(s);
printf("%d\n",ans);
return ;
}
[洛谷3808]【模板】AC自动机(简单版)的更多相关文章
- 洛谷P3808 & P3796 AC自动机模板
题目:P3808:https://www.luogu.org/problemnew/show/P3808 P3796:https://www.luogu.org/problemnew/show/P37 ...
- 洛谷 - P3966 - 单词 - AC自动机
https://www.luogu.org/problemnew/show/P3966 因为文本串就是字典本身,所以这个和平时的AC自动机不太一样.平时的query要沿着fail树把子树的出现次数依次 ...
- [模板][P3808]AC自动机(简单版)
Description: 求n个模式串中有几个在文本串中出现 Solution: 模板,详见代码: #include<bits/stdc++.h> using namespace std; ...
- 洛谷.3121.审查(AC自动机 链表)
题目链接 //删掉一个单词需要前移一段位置,用链表维护就好了 复杂度O(sum(len)) #include <cstdio> #include <cstring> #defi ...
- 洛谷 - P2444 - 病毒 - AC自动机
https://www.luogu.org/problemnew/show/P2444 有点恶心,不太明白fail的意义. #include<bits/stdc++.h> using na ...
- 洛谷 P3804 [模板] 后缀自动机
题目:https://www.luogu.org/problemnew/show/P3804 模仿了一篇题解,感觉很好写啊. 代码如下: #include<cstdio> #include ...
- 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)
To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...
- AC自动机模板1(【洛谷3808】)
题面 题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串 ...
- 洛谷.3808/3796.[模板]AC自动机
题目链接:简单版,增强版 简单版: #include <cstdio> #include <cstring> const int N=1e6+5,S=26; char s[N] ...
随机推荐
- centos7系列问题
一.CentOS7.1查看ip route有两条路由规则 1.metric值是指到达目的地需要的跳数,是表达该条路由连接质量的指标.当有多条到达相同目的地的路由记录时,路由器会采用metric值小的那 ...
- 1064 Complete Binary Search Tree (30 分)(二叉查找树)
中序遍历建树 #include<bits/stdc++.h> using namespace std; ; int s[N]; int n; int tree[N]; int cnt; v ...
- 架构师入门ing
算法竞赛水平一般,算法工程师估计遥遥无期,准备开始架构方面的学习. 单纯依靠垂直提升硬件性能来提高系统性能的时代已结束,分布式开发的时代实际上早已悄悄地成为了时代的主流. 在一个团队里,架构师充当了技 ...
- Codeforces Round #326(Div2)
CodeForces 588A 题意:Duff喜欢吃肉,想在接下来的n天,每天都有Ai斤肉吃,但每一天肉的单价Pi不定,肉 可以保存不过期,现已知n天每天肉的斤数Ai,以及单价Pi,为了使每天都 ...
- JavaWeb笔记(五)JSP
JSP 指令 格式: <%@ 指令名称 属性名1=属性值1 属性名2=属性值2 ... %> 分类: page 配置JSP页面 contentType:等同于response.setCon ...
- Java中Object.equals和String.equals的区别详解
前言 Java中的堆和常量池的区别是什么呢?Object.equals与String.equals的区别呢?下面让我们通过一个小示例让你明白它- 1.基础知识 Java的存储空间:寄存器.栈.堆.静态 ...
- java中从实体类中取值会忽略的的问题
在我们java Map中通过get来取值时会忽略的问题是:如果取得一个空值null时,那么.toString()时就会出错,而且不知道是什么原因. 现在我给的具体方法是用条件表达式先判断一下. 例: ...
- [C语言]防止头文件和全局变量重复定义
昨天下午将全局变量定义在H文件中导致链接时提示变量在多个obj文件内重复. 解决办法如下: 将变量移入C文件中进行定义,然后在H文件中加入extern在变量之前. 这样当其它C文件引用该全局变量时 ...
- POJ A Simple Problem with Integers | 线段树基础练习
#include<cstdio> #include<algorithm> #include<cstring> typedef long long ll; #defi ...
- Codeforces Round #359 (Div. 2) B
B. Little Robber Girl's Zoo time limit per test 2 seconds memory limit per test 256 megabytes input ...