题目大意:
  给定$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自动机(简单版)的更多相关文章

  1. 洛谷P3808 & P3796 AC自动机模板

    题目:P3808:https://www.luogu.org/problemnew/show/P3808 P3796:https://www.luogu.org/problemnew/show/P37 ...

  2. 洛谷 - P3966 - 单词 - AC自动机

    https://www.luogu.org/problemnew/show/P3966 因为文本串就是字典本身,所以这个和平时的AC自动机不太一样.平时的query要沿着fail树把子树的出现次数依次 ...

  3. [模板][P3808]AC自动机(简单版)

    Description: 求n个模式串中有几个在文本串中出现 Solution: 模板,详见代码: #include<bits/stdc++.h> using namespace std; ...

  4. 洛谷.3121.审查(AC自动机 链表)

    题目链接 //删掉一个单词需要前移一段位置,用链表维护就好了 复杂度O(sum(len)) #include <cstdio> #include <cstring> #defi ...

  5. 洛谷 - P2444 - 病毒 - AC自动机

    https://www.luogu.org/problemnew/show/P2444 有点恶心,不太明白fail的意义. #include<bits/stdc++.h> using na ...

  6. 洛谷 P3804 [模板] 后缀自动机

    题目:https://www.luogu.org/problemnew/show/P3804 模仿了一篇题解,感觉很好写啊. 代码如下: #include<cstdio> #include ...

  7. 洛谷P3373 [模板]线段树 2(区间增减.乘 区间求和)

    To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格 ...

  8. AC自动机模板1(【洛谷3808】)

    题面 题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串 ...

  9. 洛谷.3808/3796.[模板]AC自动机

    题目链接:简单版,增强版 简单版: #include <cstdio> #include <cstring> const int N=1e6+5,S=26; char s[N] ...

随机推荐

  1. 最小化安装Linux的常用配置整理

    基于安全性考虑,将服务器进行最小化安装,毕竟软件包越少,漏洞越少,相对来说就约安全,但是最小化安装会给运维带来一些问题和不便,下面是我总结的,常见的一些配置和工具的安装,仅供各位大神参考,如有新的id ...

  2. ssh 免交互登录 ,远程执行命令脚本。

    ##免交互SSH登录auto_login_ssh () {    expect -c "set timeout -1;                spawn -noecho ssh -o ...

  3. HttpClient实现POST参数提交

    HttpClient client = new HttpClient(); //使用FormUrlEncodedContent做HttpContent var content = new FormUr ...

  4. sqlserver 汉字转拼音 索引 函数

    IF OBJECT_ID('[fn_GetPinyin]') IS NOT NULL DROP FUNCTION [fn_GetPinyin] GO create function [dbo].[fn ...

  5. hdu 1211 RSA (逆元)

    RSA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  6. hihoCoder #1902 字符替换

    解法 这题比赛时过的人很多,我却没思路,糊里糊涂写了个强联通分量,得了 80 分. 这题思路是这样的. 一个替换操作可以看做一个有向边,所以题目实际上给出了一个有向图 $G$,一个节点代表一个字母. ...

  7. HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. 以太坊源码分析(52)以太坊fast sync算法

    this PR aggregates a lot of small modifications to core, trie, eth and other packages to collectivel ...

  9. 多校4 lazy running (最短路)

    lazy running(最短路) 题意: 一个环上有四个点,从点2出发回到起点,走过的距离不小于K的最短距离是多少 \(K <= 10^{18} 1 <= d <= 30000\) ...

  10. 洛谷 P3437 [POI2006]TET-Tetris 3D 解题报告

    P3437 [POI2006]TET-Tetris 3D 题目描述 The authors of the game "Tetris" have decided to make a ...