模板—AC自动机
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct trie
{
int count;
trie *next[],*fail;
}*q[];
int head,tail;
char keyword[];
char str[]; trie *Newtrie()
{
int i;
trie *temp=new trie;
temp->count=;
for(int i=;i<;i++)temp->next[i]=NULL;
temp->fail=NULL;
return temp;
}
void insert(trie *p,char s[])
{
int i=,index;
trie *temp=p;
while(s[i])
{
index=s[i]-'a';
if(temp->next[index]==NULL)temp->next[index]=Newtrie();
temp=temp->next[index];
i++;
}
temp->count++;
}
void build_ac(trie *root)
{
int i=,index;
q[++tail]=root;
root->fail=NULL;
while(head!=tail)
{
trie *temp=q[++head];
trie *p=NULL;
for(int i=;i<;i++)
{
if(temp->next[i]!=NULL)
{
if(temp==root) temp->next[i]->fail=root;
else
{
p=temp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL) temp->next[i]->fail=root;
}
q[++tail]=temp->next[i];
}
}
}
}
int ask(trie *root)
{
int i=,cnt=,index,len;
len=strlen(str);
trie *p=root;
while(str[i])
{
index=str[i]-'a';
while(p->next[index]==NULL && p!=root)p=p->fail;
p=p->next[index];
if(p==NULL)p=root;
trie *temp=p;
while(temp!=root && temp->count!=-)
{
cnt+=temp->count;
temp->count=-;
temp=temp->fail;
}
i++;
}
return cnt;
}
signed main()
{
int n,T;
trie *p;
cin>>T;
while(T--)
{
p=Newtrie();
cin>>n;
for(int i=;i<=n;i++)
{
cin>>keyword;
insert(p,keyword);
}
cin>>str;
build_ac(p);
cout<<ask(p)<<endl;
}
}
模板—AC自动机的更多相关文章
- luoguP3808[模板]AC自动机(简单版)
传送门 ac自动机模板题,裸的多串匹配 代码: #include<cstdio> #include<iostream> #include<algorithm> #i ...
- luoguP3796[模板]AC自动机(加强版)
传送门 ac自动机模板,可能我写的ac自动机是有点问题的,所以跑的有些慢 暴力跳fail统计 代码: #include<cstdio> #include<iostream> # ...
- 算法模板——AC自动机
实现功能——输入N,M,提供一个共计N个单词的词典,然后在最后输入的M个字符串中进行多串匹配(关于AC自动机算法,此处不再赘述,详见:Aho-Corasick 多模式匹配算法.AC自动机详解.考虑到有 ...
- 模板 AC自动机
题目描述 有$N$ 个由小写字母组成的模式串以及一个文本串$T$ .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串$T$ 中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据 ...
- 算法竞赛模板 AC自动机
AC自动机基本操作 (1) 在AC自动机中,我们首先将每一个模式串插入到Trie树中去,建立一棵Trie树,然后构建fail指针. (2) fail指针,是穿插在Trie树中各个结点之间的指针,顾名思 ...
- 洛谷.3808/3796.[模板]AC自动机
题目链接:简单版,增强版 简单版: #include <cstdio> #include <cstring> const int N=1e6+5,S=26; char s[N] ...
- 模板——AC自动机
传送门:QAQQAQ 定义nxt[u]=v表示从u开始不断沿着失配边跳到的第一个是标记点的端点v,那么我们再匹配时沿着last跳,每跳到一个last,它就一定对应一个模式串,所以效率是非常高的. 和K ...
- AC自动机例题
P3808 [模板]AC自动机(简单版) [题目描述] 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. #include<bits/stdc++.h> using name ...
- 「kuangbin带你飞」专题十七 AC自动机
layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...
随机推荐
- YTU 2642: 填空题:类模板---求数组的最大值
2642: 填空题:类模板---求数组的最大值 时间限制: 1 Sec 内存限制: 128 MB 提交: 646 解决: 446 题目描述 类模板---求数组的最大值 找出一个数组中的元 ...
- 解决vs2010无法找到System.Data.OracleClient的引用问题
解决vs2010无法找到System.Data.OracleClient的引用问题 2012-2-19 09:12| 发布者: benben| 查看: 7627| 评论: 0 摘要: 在vs201 ...
- bzoj1085 [SCOI2005]骑士精神——IDA*
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 搜索,IDA*,估价就是最少需要跳的步数: 代码意外地挺好写的,memcmp 用起来好 ...
- bzoj1016 [JSOI2008]最小生成树计数——Kruskal+矩阵树定理
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1016 从 Kruskal 算法的过程来考虑产生多种方案的原因,就是边权相同的边有一样的功能, ...
- 手推FP-growth (频繁模式增长)算法------挖掘频繁项集
一.频繁项集挖掘为什么会出现FP-growth呢? 原因:这得从Apriori算法的原理说起,Apriori会产生大量候选项集(就是连接后产生的),在剪枝时,需要扫描整个数据库(就是给出的数据),通过 ...
- ubuntu16.04 查看CPU是几核
ubuntu 16.04下查看机器是cpu是几核的 几个cpu more /proc/cpuinfo |grep "physical id"|uniq|wc -l 每个cpu是几核 ...
- 【149】ArcGIS Desktop 10.0 & Engine 10.0 安装及破解
写在前面:可能会出现按照此方法无法破解的情况,那请确保您有将 ArcGIS 10.0 已经完全卸载干净,直接通过控制面板进行卸载的时候并不能将其卸载干净,需要进行更深层次的卸载,包括删除注册表,各种文 ...
- sqlserver2000连接失败,不存在或拒绝访问
一 看ping 服务器IP能否ping通. 这个实际上是看和远程sql server 2000服务器的物理连接是否存在.如果不行,请检查网络,查看配置,当然得确保远程sql server 2000服务 ...
- .net中RSA加密解密
1.产生密钥: private static void CreateKey() { using (RSACryptoServiceProvider rsa = new RSACryptoService ...
- Android 性能优化(15)网络优化( 11)Manipulating Broadcast Receivers On Demand
Manipulating Broadcast Receivers On Demand This lesson teaches you to Toggle and Cascade State Chang ...