#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. 利用栈Stack实现队列(Queue)

    实现说明: 入队时,将元素压入s1; 出队时,推断s2是否为空,如不为空,则直接弹出顶元素:如为空.则将s1的元素逐个"倒入"s2.把最后一个元素弹出并出队; 这个思路,避免了重复 ...

  2. 深入浅出Hibernate(二)多对一关系映射

    学习Hibernate是为了更方便的操作数据库,在数据库中的关系模型中存在多对一的关系,比方下图所看到的的员工和部门之间的关系,那么这样的关系在Hibernate中怎样映射呢?让我用一个小Demo来具 ...

  3. 代理ip 测试

    Line #1218 : 101.232.208.245 - - [16/Jan/2018:02:47:34 +0800] "GET /?xltestdesfs HTTP/1.1" ...

  4. 在java中除去字符串(String)中的换行字符(\r \n \t)

    我们先来看几个例子: 例1: public class Test { public static void main(String[] args) { String s = "'sds gd ...

  5. (数论)51NOD 1079 中国剩余定理

    一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K.例如,K % 2 = 1, K % 3 = 2, K % 5 = 3.符合条件的最小的K = 23.   Input 第1行:1个数 ...

  6. ----堆栈 STL 函数库 ----有待补充

    #include<cstdio> #include<string> #include<vector> #include<iostream> using ...

  7. 洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)

    题面 传送门 题解 日常敲板子.jpg //minamoto #include<bits/stdc++.h> #define R register #define inline __inl ...

  8. js复制功能

    // 复制功能 copyUrl() { var Url = document.getElementById('biao') Url.select() // 选择对象 document.execComm ...

  9. view 的继承关系

    view 的继承关系 如果一个view 有多个子view. 甚至是多成次的 子view. 只需要把第一级别的view(父view) 授权给用户,就可以访问view了,子view 不需要授权.

  10. Using 10053 Trace Events and get outline

    When it comes to performance tuning, we can spend time on one or both ends of the problem. On the &q ...