只是记录一下代码

AC自动机算法的教程请移步这里

还有这里

指针看着懵逼的还可以看一下这里

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 10010
#define maxl 1000100
using namespace std;
int n;
char s[maxl];
struct ac_automation
{
int tot;
struct node
{
node *son[];//记录儿子
int size;//儿子数
node *fail;//失配指针
node ()
{
memset(this,,sizeof(node));
}
};
node *root;
inline void init()
{
root=new node();
}
inline void insert()
{
int l=strlen(s+),i=;
node *now=root;
while(i<=l)
{
if(!now->son[s[i]-'a'])now->son[s[i]-'a']=new node();
now=now->son[s[i]-'a'];
i++;
}
now->size++;
}
inline void build()
{
queue <node*> q;
for(int i=;i<;i++)
{
if(root->son[i])
{
q.push(root->son[i]);
root->son[i]->fail=root;
}
else root->son[i]=root;
}
while(!q.empty())
{
node *x=q.front();
q.pop();
for(int i=;i<;i++)
{
if(x->son[i])
{
x->son[i]->fail=x->fail->son[i];
q.push(x->son[i]);
}
else x->son[i]=x->fail->son[i];
}
}
}
inline int query()
{
node *now=root;
int i=,l=strlen(s+),ans=;
while(i<=l)
{
now=now->son[s[i]-'a'];
for(node *j=now;j!=root&&j->size!=-;j=j->fail)
{
ans+=j->size;
j->size=-;
}
i++;
}
return ans;
}
}ac;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ac.init();//千万别忘了
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",s+);
ac.insert();
}
ac.build();
scanf("%s",s+);
printf("%d\n",ac.query());
}
return ;
}

[AC自动机模板]Keywords Search的更多相关文章

  1. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  2. AC自动机(Keywords Search)

    题目链接:https://cn.vjudge.net/contest/280743#problem/A 题目大意:首先给你T组测试样例,然后给你n个字符串,最后再给你一个模式串,然后问你这一些字符串中 ...

  3. 【AC自动机】Keywords Search

    [题目链接] https://loj.ac/problem/10057 [题意] 原题来自:HDU 2222 给定  n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文 ...

  4. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  6. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  7. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

  8. KMP与AC自动机模板

    HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...

  9. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

随机推荐

  1. 南阳OJ独木舟上的旅行

     /*独木舟上的旅行 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描写叙述 进行一次独木舟的旅行活动.独木舟能够在港口租到,而且之间没有差别. 一条独木舟最多仅仅能乘坐 ...

  2. vuex资料

    vuex最简单.最详细的入门文档 链接:https://segmentfault.com/a/1190000009404727 https://www.jb51.net/article/138239. ...

  3. javascript的继承方法

    一.构造函数继承 该方法最简单,使用call或者apply方法,将父对象的构造函数绑定到子对象上. function Parent(name){ this.name = name; this.colo ...

  4. 【MySQL】Win7下修改MySQL5.5默认编码格式

    一般安装MySQL程序过程中,有一步骤是选择MySQL的默认编码格式的,程序默认为Latin1编码格式,当然也可以选择第三个选项,手动选择gbk或utf8编码格式,以支持中文数据.如下图: 现在问题出 ...

  5. shell脚本 加密备份MySQL数据库

    1.加密备份为.bak文件(实际只是个.zip文件) #!/bin/bash # $:IP地址 # $:用户名 # $:数据库密码 # $:数据库名 # $:加密密码 # $:备份文件名 mysqld ...

  6. PromiseKit入门

    原文:Getting Started with PromiseKit 作者:Michael Katz 译者:kmyhy 异步编程真的让人头疼.不管你怎样小心,总是easy出现臃肿的托付.混乱的完毕句柄 ...

  7. linux设备驱动归纳总结(八):2.match.probe.remove

    linux设备驱动归纳总结(八):2.总线.设备和驱动的关系 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  8. MySQL测试代码

    MySQL测试代码 # 注释内容 -- 注释内容 -- 创建maizi数据库 CREATE DATABASE IF NOT EXISTS `maizi` DEFAULT CHARACTER SET ' ...

  9. [luogu2620]虫洞

    https://www.zybuluo.com/ysner/note/1284536 题面 给一个一维坐标系,出发点为\(0\),目标点为\(w\). 每\(1\)秒可以往后移不超过\(s\)个单位距 ...

  10. 洛谷 P1351 联合权值 —— 树形DP

    题目:https://www.luogu.org/problemnew/show/P1351 树形DP,别忘了子树之间的情况(拐一下距离为2). 代码如下: #include<iostream& ...