模板—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 ...
随机推荐
- Scala界面事件处理
演示样例代码: import scala.swing.SimpleSwingApplication import scala.swing.MainFrame import scala.swing.Bu ...
- Android实战技巧之八:Ubuntu下切换JDK版本【转】
本文转载自:http://blog.csdn.net/lincyang/article/details/42024565 Android L之后推荐使用JDK7编译程序,这是自然发展规律,就像是4年前 ...
- 8-12 canvas专题-阶段练习一(上)
8-12 canvas专题-阶段练习一(上) <!DOCTYPE html> <html lang="zh-cn"> <head> <me ...
- bzoj 1596: [Usaco2008 Jan]电话网络【贪心】
dfs,如果一个点的儿子.本身.父亲都没有塔,就在父亲上建一个 原理不明-- #include<iostream> #include<cstdio> using namespa ...
- 指向“”的 script 加载失败
今天遇到了一个非常奇怪的问题:在某个同时的电脑上,所有浏览器无法打开某个页面,F12查看控制台,发现有一个黄色的 指向“xxxx.js”的 <script> 加载失败 的提示.该外部js文 ...
- 洛谷 P3378 【模板】堆(小根堆)
题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...
- JQ 获取Table的td 值
<script type="text/javascript"> function SetTable() { $("#myTab table").ea ...
- 生成自签名ca 证书 使nginx 支持https
创建服务器私钥,命令会让你输入一个口令:$ openssl genrsa -des3 -out server.key 1024创建签名请求的证书(CSR):$ openssl req -new -ke ...
- json常识
转载网址:http://developer.51cto.com/art/201704/536386.htm 我们先来看一个JS中常见的JS对象序列化成JSON字符串的问题. 请问:以下JS对象通过 ...
- git删除本地分支失败,报错error: branch 'test219' not found.
错误: 删除本地分支报错,操作如下: git branch -d test219 操作失败,错误信息:error: branch 'test219' not found git branch -D t ...