模板

\(Problem:\) 求 \(n\) 个模式串在文本串中出现的次数

\(templete:\) \(Luogu5357\)

\(Code\)

#include<cstdio>
#include<cstring>
using namespace std; const int N = 2e5 + 5;
int tr[N][26] , sum[N * 26] , id[N * 26] , tag[N * 26] , fail[N * 26] , q[N * 26] , ans[N] , map[N];
int n , tot;
char s[2000005]; void insert(int x)
{
int len = strlen(s) , u = 0;
for(register int i = 0; i < len; i++)
{
int ch = s[i] - 'a';
if (!tr[u][ch]) tr[u][ch] = ++tot;
u = tr[u][ch];
}
if (!id[u]) id[u] = x;
map[x] = id[u];
} void get_fail()
{
int h = 0 , t = 0;
for(register int i = 0; i < 26; i++) if (tr[0][i]) q[++t] = tr[0][i];
while (h < t)
{
int now = q[++h];
for(register int i = 0; i < 26; i++)
{
if (!tr[now][i]) {tr[now][i] = tr[fail[now]][i]; continue;}
fail[tr[now][i]] = tr[fail[now]][i];
++tag[tr[fail[now]][i]] , q[++t] = tr[now][i];
}
}
} void calc()
{
int len = strlen(s) , u = 0;
for(register int i = 0; i < len; i++)
u = tr[u][s[i] - 'a'] , ++sum[u];
int h = 0 , t = 0;
for(register int i = 1; i <= tot; i++)
if (!tag[i]) ans[id[i]] = sum[i] , q[++t] = i;
while (h < t)
{
int now = q[++h] , k = fail[now];
--tag[k] , sum[k] += sum[now] , ans[id[k]] = sum[k];
if (!tag[k]) q[++t] = k;
}
} int main()
{
scanf("%d" , &n);
for(register int i = 1; i <= n; i++) scanf("%s" , s) , insert(i);
get_fail();
scanf("%s" , s) , calc();
for(register int i = 1; i <= n; i++) printf("%d\n" , ans[map[i]]);
}

【模板】AC自动机(二次加强版)的更多相关文章

  1. luoguP3796[模板]AC自动机(加强版)

    传送门 ac自动机模板,可能我写的ac自动机是有点问题的,所以跑的有些慢 暴力跳fail统计 代码: #include<cstdio> #include<iostream> # ...

  2. luoguP3808[模板]AC自动机(简单版)

    传送门 ac自动机模板题,裸的多串匹配 代码: #include<cstdio> #include<iostream> #include<algorithm> #i ...

  3. UVA 11019 Matrix Matcher ( 二维字符串匹配, AC自动机 || 二维Hash )

    题目: 传送门 题意: 给你一个 n * m 的文本串 T, 再给你一个 r * c 的模式串 S: 问模式串 S 在文本串 T 中出现了多少次. 解: 法一: AC自动机 (正解) 670ms 把模 ...

  4. 算法模板——AC自动机

    实现功能——输入N,M,提供一个共计N个单词的词典,然后在最后输入的M个字符串中进行多串匹配(关于AC自动机算法,此处不再赘述,详见:Aho-Corasick 多模式匹配算法.AC自动机详解.考虑到有 ...

  5. 模板 AC自动机

    题目描述 有$N$ 个由小写字母组成的模式串以及一个文本串$T$ .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串$T$ 中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据 ...

  6. pku1204 Word Puzzles AC自动机 二维字符串矩阵8个方向找模式串的起点坐标以及方向 挺好的!

    /** 题目:pku1204 Word Puzzles 链接:http://poj.org/problem?id=1204 题意:给定一个L C(C <= 1000, L <= 1000) ...

  7. 算法竞赛模板 AC自动机

    AC自动机基本操作 (1) 在AC自动机中,我们首先将每一个模式串插入到Trie树中去,建立一棵Trie树,然后构建fail指针. (2) fail指针,是穿插在Trie树中各个结点之间的指针,顾名思 ...

  8. UVa 11019 (AC自动机 二维模式串匹配) Matrix Matcher

    就向书上说得那样,如果模式串P的第i行出现在文本串T的第r行第c列,则cnt[r-i][c]++; 还有个很棘手的问题就是模式串中可能会有相同的串,所以用repr[i]来记录第i个模式串P[i]第一次 ...

  9. AC自动机(二维) UVA 11019 Matrix Matcher

    题目传送门 题意:训练指南P218 分析:一行一行的插入,一行一行的匹配,当匹配成功时将对应子矩阵的左上角位置cnt[r][c]++;然后统计 cnt[r][c] == x 的数量 #include ...

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

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

随机推荐

  1. c++详细学习——继承

    通常讲父类(parrent)-子类(child).基类(base)-派生类(derived)和超类(super)-子类(sub) 1 最基础的写法 以下例子为最基本的写法,默认构造 1 enum Ge ...

  2. crtl+鼠标左键代码出现class file editor,source not found

    点击Attached source或者已经添加过按钮变成Change Attached Source 点击之后将自己jdk路径下的src.zip导入就可以了 图片来自https://blog.csdn ...

  3. 【JUC】交换器Exchanger详解

    欢迎关注专栏[JAVA并发] 前言 JDK中提供了不少的同步工具,现在分享一个相对比较冷门的同步工具--交换器(Exchanger).你知道Exchanger的作用是什么吗?实现机制是什么?可以用来做 ...

  4. bug处理记录:com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 9)): has to be escaped using backslash to be included in string value at [Source:

    1. 报错: com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 9 ...

  5. 动态SQL遇到的问题

    看图 查不出来任何数据 因为判断有问题 修改方法如下:

  6. kali使用命令ifconfig查询ip地址一直为127.0.0.1的解决办法

    解决方法: 执行命令:dhclient -v,即可解决

  7. 2022年7月14日,第四组 周鹏,认识JAVA的第二天(;´д`)ゞ(;д;)

    那天,我遇到了JAVA 然后,我失去了头发 无论我用了多少办法 还是放不下那个它 我哭的像个傻瓜 但也没能留住它 如果再有一次从来 我愿为它披上薄纱 愿它安稳有个家 可我终究还是失去了它 失去了原本为 ...

  8. LeetCode HOT 100:验证二叉搜索树(从左右子树获取信息进行推导)

    题目:98. 验证二叉搜索树 题目描述: 给你一个二叉树,让你判断该二叉树是否是二叉搜索树.什么是二叉搜索树呢?就是某一个节点的左子树上的所有节点的值都小于当前节点,右子树上的所有节点值都大于当前节点 ...

  9. [LeetCode]二进制求和

    题目 代码 class Solution { public: string addBinary(string a, string b) { int lenA = a.length(); int len ...

  10. Xversion 在 macOS12.4

    很多同学发现在macOS12.4上已经不用使用cornerstone 4的svn了 针对macOS12.4,Xversion照样能使用 但是有些时候我们需要提交.a文件,Xversion中又看不到,针 ...