#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自动机的更多相关文章

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

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

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

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

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

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

  4. 模板 AC自动机

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

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

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

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

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

  7. 模板——AC自动机

    传送门:QAQQAQ 定义nxt[u]=v表示从u开始不断沿着失配边跳到的第一个是标记点的端点v,那么我们再匹配时沿着last跳,每跳到一个last,它就一定对应一个模式串,所以效率是非常高的. 和K ...

  8. AC自动机例题

    P3808 [模板]AC自动机(简单版) [题目描述] 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. #include<bits/stdc++.h> using name ...

  9. 「kuangbin带你飞」专题十七 AC自动机

    layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...

随机推荐

  1. FreeRTOS系列第14篇---FreeRTOS任务通知

    注:本文介绍任务通知的基础知识,具体源代码分析见<FreeRTOS高级篇8---FreeRTOS任务通知分析> 每一个RTOS任务都有一个32位的通知值,任务创建时,这个值被初始化为0.R ...

  2. iOS开发——高级篇——线程同步、线程依赖、线程组

    前言 对于iOS开发中的网络请求模块,AFNet的使用应该是最熟悉不过了,但你是否把握了网络请求正确的完成时机?本篇文章涉及线程同步.线程依赖.线程组等专用名词的含义,若对上述名词认识模糊,可先进行查 ...

  3. ios34---GDC,dispatch_once

    // // ViewController.m // 09-掌握-GCD常用函数 // // Created by xiaomage on 16/2/18. // Copyright © 2016年 小 ...

  4. Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation

    C. Fox and Box Accumulation time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. openstack dnsmasq

    killall dnsmasq systemctl restart openstack-nova-compute /sbin/dnsmasq --conf-file=/var/lib/libvirt/ ...

  6. Python 返回多个值+Lambda的使用

    def MaxMin(a,b): if(a>b): return a,b else: return b,a max,min=MaxMin(8,95) print "最大值为:" ...

  7. 14. extjs中treepanel属性和方法

    转自:http://www.cnblogs.com/connortang/p/4414907.html 1.Ext.tree.TreePanel 主要配置项: root:树的根节点. rootVisi ...

  8. http-2.4

    http-2.4 1)新特性 (1)MPM 支持运行为DSO 机制:以模块形式按需加载 (2)event MPM 生产环境可用 (3)异步读写机制 (4)支持每模块及每目录的单独日志级别定义 (5)每 ...

  9. 学习http协议的三次握手和四次挥手 ~~笔记

    http协议是基于tcp协议的  所以应该说是tcp协议的三次握手和四次挥手 SYN:请求建立连接,并在其序列号的字段进行序列号的初始值设定.建立连接,设置为1 FIN:用来释放一个连接.FIN=1表 ...

  10. eclipse mybatis 快速生成工具

    1.首先,得先看看eclipse有没安装mybatis generator插件,如果有的话,请忽略这一步 eclipse在线安装mybatis generator 1.打开eclipse,找到help ...