只是记录一下代码

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. 安装eclipse中html/jsp/xml editor插件以及改动html页面的字体

    近期在做android项目,用到了jquery mobile 框架以及phonegap,所以就会涉及一些html文件,可是html文件打开的方式是Text Editor ,而且打开之后一些html代码 ...

  2. Guava ---- EventBus事件驱动模型

    在软件开发过程中, 难免有信息的共享或者对象间的协作. 怎样让对象间信息共享高效, 而且耦合性低. 这是一个难题. 而耦合性高将带来编码改动牵一发而动全身的连锁效应. Spring的风靡正是由于攻克了 ...

  3. Noip模拟 Day6.12

    第一题:贪吃蛇(snake) 本题其实就是判断一个有向图中有没有环,做一次拓扑排序就可以了,如果所有点都入队了,就表示没有环,否则就有环.或者就是dfs一次,每个点只需要被访问一次,这样也是O(n)的 ...

  4. 【codevs2183】匹配字符串

    KMP裸题 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring ...

  5. 【Noip模拟By yxj】

    1.randomDescription 给定4个参数A0,N,c,p,你需要按下式构造A1~AN: A[i]=(A[i-1]2+c)mod p 之后,你需要求出A1~AN中,第K大的数值.Input ...

  6. servlet container:tomcat jetty and undertow

    1 spring boot内嵌容器支持tomcat.jetty和undertow 但是undertow性能最好,详见: https://examples.javacodegeeks.com/enter ...

  7. 【HDU 1005】 Number Sequence

    [题目链接] 点击打开链接 [算法] 矩阵乘法快速幂,即可 [代码] #include<bits/stdc++.h> using namespace std; int a,b,n; str ...

  8. 6 WPF控件

    WPF控件分类: 内容控件 标题内容控件 文本控件 列表控件 基于范围的控件 日期控件 控件类 控件是与用户交互的元素.控件可以获得焦点,能接受键盘或鼠标的输入. 所有控件的基类是System.Win ...

  9. win7 32位解决matlab out of memory问题

    由于最近在做DL,matlab load数据时由于内存只有2G,会出现out of memory的情况,网上百度了下都是在xp下打开3GB来解决该问题,但是由于win7没有boot.ini无法在win ...

  10. 让你彻底明白JAVA中堆与栈的区别

    原文地址:http://www.2cto.com/kf/201302/190704.html 简单的说: Java把内存划分成两种:一种是栈内存,一种是堆内存. 在函数中定义的一些基本类型的变量和对象 ...