[洛谷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] ...
随机推荐
- linux ubuntu开启sshd
which ssh #查看文件 sudo apt-get install ssh #安装ssh cd /etc/init.d #切换目录 ls -l | grep ssh #执行启动脚本 sudo s ...
- Oracle 学习----游标(使用带参光标cursor)
--表参照无参光标页信息 --注意:红色就是参数 declare cursor tt(pkeycode temp.keycode%type) is select name,sal from temp ...
- win10激活(转)
批处理命令激活方法,此方法和激活码可激活 180天 先来说下使用激活码使用方法: 1.同时按下Win键+X,然后选择命令提示符(管理员) 2.在命令提示符中依次输入: slmgr.vbs /upk ( ...
- Python 3.x的编码问题
Python 3的源码.py文件的默认编码方式为UTF-8(Python 2.x的默认编码格式为unicode). encode的作用,使我们看到的直观的字符转换成计算机内的字节形式. decode刚 ...
- SVN客户端使用手册
使用svn进行源代码版本控制,代码管理利器. 优点: 使用方便,与文件管理器集成.速度快,稳定. 实现代码比较,比如对历史和当前代码进行比较. 解决多人同时编写代码时代码重复修改困难. 安装: 下载网 ...
- js文字效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- idea中新建的web项目不能新建servlet
============ ======================================================================== 在新建的“ java ”源文 ...
- 百度之星初赛(A)——T2
数据分割 小w来到百度之星的赛场上,准备开始实现一个程序自动分析系统. 这个程序接受一些形如x_i = x_jxi=xj 或 x_i \neq x_jxi≠xj 的相等/不等约 ...
- Java I/O 笔记
1. Java常用I/O类概述 2. 文件I/O 你可以根据该文件是二进制文件还是文本文件来选择使用FileInputStream(FileOutputStream)或者FileReader(File ...
- 生成DLL
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...