#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. Python按行输出文件内容具体解释及延伸

    下面两端測试代码分别为笔者所写,第一段为错误版本号.后者为正确版本号: #! /usr/bin/python2.7 try:     filename = raw_input('please inpu ...

  2. java jxl读取excel中Date类型

    Workbook book = Workbook.getWorkbook(excel); Sheet sheet = book.getSheet(0); int clos = sheet.getCol ...

  3. PNG vs. GIF vs. JPEG vs. SVG - When best to use?

    image - PNG vs. GIF vs. JPEG vs. SVG - When best to use? - Stack Overflow https://stackoverflow.com/ ...

  4. 【转】React 常用面试题目与分析

    作者:王下邀月熊链接:https://zhuanlan.zhihu.com/p/24856035来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文有一定概率为水文,怕 ...

  5. MATLAB——matlab特殊符号表【转载】

    链接来源: matlab特殊符号表 http://blog.sina.com.cn/s/blog_4a09187801014xg9.html Character Sequence Symbol Cha ...

  6. YTU 2641: 填空题:静态成员---计算学生个数

    2641: 填空题:静态成员---计算学生个数 时间限制: 1 Sec  内存限制: 128 MB 提交: 267  解决: 206 题目描述 学生类声明已经给出,在主程序中根据输入信息输出实际建立的 ...

  7. 【HDU 2157】 How Many Ways??

    [题目链接] 点击打开链接 [算法] 设A[i][j]为走一条边,从i走到j的方案数 C[i][j]为走两条边,从i走到j的方案数,显然有 : C = A * A = A^2 C'[i][j]为走三条 ...

  8. 7 Worksheet 对象

    7.1 设置阶段 代码清单7.1:使用Parent属性获得一个对象的父对象的指针 '使用Parent属性获得一个对象的父对象的指针 Sub MeetMySingleParent() 'Declare ...

  9. ubuntu16.04 查看CPU是几核

    ubuntu 16.04下查看机器是cpu是几核的 几个cpu more /proc/cpuinfo |grep "physical id"|uniq|wc -l 每个cpu是几核 ...

  10. matlab绘制曲线对比图

    >> clear;>> x1=[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8];>> y1=[0,0.55,0.69,0.86,0.93,0. ...