只是记录一下代码

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. 图像处理之基础---yuv420及其rgb,bayer, yuv, RGB的相互转换详解

    YUV格式解析1(播放器——project2) 根据板卡api设计实现yuv420格式的视频播放器 打开*.mp4;*.264类型的文件,实现其播放. 使用的视频格式是YUV420格式   YUV格式 ...

  2. Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)

                                                     D. Dima and Lisa Dima loves representing an odd num ...

  3. SNAPSHOTTING

    /etc/redis.conf ################################ SNAPSHOTTING ################################## Sav ...

  4. Windows10、ARM开发板、VMware虚拟机同时连接Internet

    前段时间有人遇到一些网络连接问题,让我帮忙处理,他想让ARM开发板连接外网,可以连接网络数据库,同时保证自己的电脑可以上网. 本来说直接可以连接一个路由器,分配一个内网IP给ARM就可以了,但是当时那 ...

  5. 蓝牙驱动分析 linux

    蓝牙驱动分析 这个驱动分析的是OK6410开发板自带的内核版本是linux3.0.1,所支持的wifi和蓝牙一体芯片是marvell的8688和8787.根据开发板的设计,芯片与主机之间是通过sdio ...

  6. Interfaces (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...

  7. Cocos2dx如何引用第三方SO文件(Android NDK)

    做项目的过程中发现,引用第三方的库lib3rdsdk.so,当直接把lib3rdsdk.so放进armeabi文件夹里,会被删除掉.查网上资料都说的不全,经过实验,最简单的方法就是在jni下的andr ...

  8. loj 6034 线段游戏

    题目大意: 给出若干条线段,用 (x1,y2),(x2,y2) 表示其两端点坐标,现在要求支持两种操作: 0 x1 y1 x2 y2 表示加入一条新的线段 (x1,y2),(x2,y2) 1 x0 询 ...

  9. 【Unity3D】Unity3D SkinnedMeshRenderer换装系统

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/6505561.html 一.换装原理 游戏角色换装分为以下几步: 1.替换蒙皮网格 2.刷新骨骼 3.替换材质 上 ...

  10. bzoj 1059: [ZJOI2007]矩阵游戏【匈牙利算法】

    注意到怎么换都行,但是如果把某个黑方块用在对角线上,它原来所在的行列的的黑方块就都不能用 所以要选出n组不重的行列组合,这里用匈牙利算法做二分图匹配即可(用了时间戳优化) #include<io ...