链接:

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自动机(模板)的更多相关文章

  1. 洛谷P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

  2. 【刷题】洛谷 P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

  3. 浅谈AC自动机模板

    什么是AC自动机? 百度百科 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法. 要学会AC自动机,我们必须知道什么是Trie,也就是字典树.Tr ...

  4. (模板)AC自动机模板

    模板1. 给出模式串和文本串,文本串长度小于1e6,模式串长度之和小于1e6,求文本串中有多少模式串出现. 题目链接:https://www.luogu.org/problem/P3808 AC co ...

  5. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  6. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  7. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  8. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  9. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  10. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

随机推荐

  1. 什么是弹性公网IP?

    弹性公网IP(Elastic IP Address,简称EIP),是可以独立购买和持有的公网IP地址资源.目前,EIP可绑定到专有网络类型的ECS实例.专有网络类型的私网SLB实例.专有网络类型的辅助 ...

  2. 【AtCoder】AGC003

    AGC编号越小越水???? AGC003 A - Wanna go back home 相对方向要么一起有要么一起没有 #include <bits/stdc++.h> #define f ...

  3. Scala当中什么是RDD(Resilient Distributed Datasets)弹性分布式数据集

    RDD(Resilient Distributed Datasets)弹性分布式数据集.你不好理解的话,可以把RDD就可以看成是一个简单的"动态数组"(比如ArrayList),对 ...

  4. Python第三方库资源

    [转载]Python第三方库资源   转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github ...

  5. k8s之调度器、预选策略及优选函数

    1.调度器(scheduler) 调度器的功能是调度Pod在哪个Node上运行,这些调度信息存储在master上的etcd里面,能够和etcd打交道的只有apiserver; kubelet运行在no ...

  6. git merge 命令的使用

    我们把dev分支的工作成果合并到master分支上: $ git merge dev Updating d46f35e..b17d20e Fast-forward readme.txt | 1 + 1 ...

  7. 事件处理程序EventUtil

    /**********事件处理程序***********EventUtil.js*浏览器兼容,<高三>13章 P354*2014-12-8************************* ...

  8. 红黑树和AVL树

    在此之前,我没有了解过红黑树以及AVL tree,真是孤陋寡闻.如果你也在学习的话,我们一起进步. 如果,你很急,那么只看红色加粗即可. 1.红黑树(RB-tree) 红黑树是一种特殊的二叉搜索树,特 ...

  9. 7.Spring整合Hibernate_1

    Spring 整合 Hibernate 1.Spring指定 database,给下面创建的 SessionFactory用 <!-- !!!!!可以使用 @Resource 将 这个bean对 ...

  10. php的三个常用判断函数

    <?phperror_reporting(E_ERROR);$a;$b = false;$c = '';$d = 0;$e = null;$f = array(); echo 'empty', ...