洛谷-P3808-AC自动机(模板)
链接:
https://www.luogu.org/problem/P3808
题意:
给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过。
思路:
模板,
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 2e6+10;
struct Trie
{
int Next[26];
int End;
int Fail;
void Init()
{
memset(Next, 0, sizeof(Next));
End = Fail = 0;
}
}trie[MAXN];
int cnt, n;
char s[MAXN];
void Insert(char *t)
{
int len = strlen(t);
int p = 0;
for (int i = 0;i < len;i++)
{
if (trie[p].Next[t[i]-'a'] == 0)
{
trie[p].Next[t[i]-'a'] = ++cnt;
trie[cnt].Init();
}
p = trie[p].Next[t[i]-'a'];
}
trie[p].End++;
}
void BuildAC()
{
queue<int> que;
for (int i = 0;i < 26;i++)
{
if (trie[0].Next[i] != 0)
que.push(trie[0].Next[i]);
}
while (!que.empty())
{
int u = que.front();
que.pop();
for (int i = 0;i < 26;i++)
{
if (trie[u].Next[i])
{
trie[trie[u].Next[i]].Fail = trie[trie[u].Fail].Next[i];
que.push(trie[u].Next[i]);
}
else
trie[u].Next[i] = trie[trie[u].Fail].Next[i];
//压缩路径, 没有的点直接指向别的节点, 减少向上找的时间
}
}
}
int Query(char *qs)
{
int len = strlen(qs);
int p = 0, ans = 0;
for (int i = 0;i < len;i++)
{
p = trie[p].Next[qs[i]-'a'];
for (int j = p;j != 0 && trie[j].End != -1;j = trie[j].Fail)
{
//将所有能出现的匹配都跑一遍, 同时处理防止多跑.
ans += trie[j].End;
trie[j].End = -1;
}
}
return ans;
}
int main()
{
cnt = 0;
scanf("%d", &n);
trie[0].Init();
for (int i = 1;i <= n;i++)
{
scanf("%s", s);
Insert(s);
}
BuildAC();
scanf("%s", s);
printf("%d\n", Query(s));
return 0;
}
洛谷-P3808-AC自动机(模板)的更多相关文章
- 洛谷P3808 【模板】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
- 【刷题】洛谷 P3808 【模板】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
- 浅谈AC自动机模板
什么是AC自动机? 百度百科 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法. 要学会AC自动机,我们必须知道什么是Trie,也就是字典树.Tr ...
- (模板)AC自动机模板
模板1. 给出模式串和文本串,文本串长度小于1e6,模式串长度之和小于1e6,求文本串中有多少模式串出现. 题目链接:https://www.luogu.org/problem/P3808 AC co ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
随机推荐
- MongoDB使用过程中的报错处理(持续更新)
1.连接池问题 com.mongodb.DBPortPool$SemaphoresOut Concurrent requests for database connection have exceed ...
- centos7 虚拟机 A start job is running for /etc/rc.d/rc.local Comp。。。
一直卡这F5查看日志,最后一行出现A start job is running for /etc/rc.d/rc.local Comp... 原因是rc.local权限设错了解决方法:1.进入单用户模 ...
- Elasticsearch使用小结之冷热分离
Elasticsearch使用小结之冷热分离 索引迁移 索引setting中的index.routing.allocation.exclude和index.routing.allocation.inc ...
- Kubernetes---Service(SVC)服务--ingress api
对于k8s传统的svc来说 它仅支持4层代理,如果遇到7层代理的话,是没有办法去实现的 k8s官方在1.11中推出了ingress api接口,通过ingress达到7层代理的效果 对于ingress ...
- Pygame小游戏练习五
@Python编程从入门到实践 Python项目练习 十一.显示游戏得分及最高分 创建新类Scoreboard,用以显示得分和最高分. # scoreboard.py import pygame.fo ...
- 这里除了安全,什么都不会发生!Docker镜像P2P加速之路
1.1 问题: 在使用Docker运行容器化应用时,宿主机通常先要从Registry服务(如Docker Hub)下载相应的镜像(image).这种镜像机制在开发环境中使用还是很有效的,团队 ...
- Wannafly挑战赛22
B. 字符路径 给一个含n个点m条边的有向无环图(允许重边,点用1到n的整数表示),每条边上有一个字符,问图上有几条路径满足路径上经过的边上的字符组成的的字符串去掉空格后以大写字母开头,句号 '.' ...
- 使用X.509数字证书加密解密实务(二)-- 使用RSA证书加密敏感数据
一. 使用RSA证书加.解密敏感数据 X.509证书标准支持三种不对称加密算法:RSA, DSA, Diffie-Hellman algorithms.最常用的是RSA算法.所以本文就以前面章节使用 ...
- __imp__SetupDiDestroyDeviceInfoList
error LINK2019 unresolved external symbol __imp__SetupDiDestroyDeviceInfoList 分类: 转载文章2012-11-02 15: ...
- get_object_or_404返回404(400)
get_object_or_404:第一个参数为queryset,第二个参数必须以关键字的形式传递,否则报错