[AC自动机模板]Keywords Search
只是记录一下代码
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的更多相关文章
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- AC自动机(Keywords Search)
题目链接:https://cn.vjudge.net/contest/280743#problem/A 题目大意:首先给你T组测试样例,然后给你n个字符串,最后再给你一个模式串,然后问你这一些字符串中 ...
- 【AC自动机】Keywords Search
[题目链接] https://loj.ac/problem/10057 [题意] 原题来自:HDU 2222 给定 n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文 ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- KMP与AC自动机模板
HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
随机推荐
- OpenLayers 3+Geoserver+PostGIS实现点击查询
WebGIS开发中,点击查询是最经常使用的一种查询方式,在ArcGIS api 中.这样的查询叫IdentifyTask,主要作用是前台提交參数.交ArcServer查询分析返回. 本文从开源框架的角 ...
- oracle导入命令,记录一下 数据库日志太大,清理日志文件
oracle导入命令,记录一下 工作中用到了,这个命令,记录一下,前提要安装imp.exe imp PECARD_HN/PECARD_HN@127.0.0.1:1521/orcl file=E:\wo ...
- cocos2d-x2.2.5走四棋儿源代码“开源”
尊重开发人员的劳动成果.转载请注明From郝萌主 游戏简单介绍: 一款益智棋类游戏,通过两枚棋子对上敌方的一枚棋子便可击杀对方. 游戏界面精美简洁,游戏规则简单明了,AI聪明有趣. 人人对战,人机对战 ...
- Paypal支付(一)MPL真正的快捷支付
一.前导 前面讲到了MEC支付,是在Web端集成好的,在手机端仅仅需通过WebView进行载入就可以,不须要不论什么Paypal第三方架包.以下将的是MPL支付.须要架包. 这样的支付的形式能够參考以 ...
- Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学题
C. Vasya and Petya's Game ...
- hdoj--1254--推箱子(bfs好题)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Java 并发 —— Thread、Executor、线程池
Java 线程池: ThreadPoolExecutor,创建此线程池的方法: new Executors.newCachedThreadPool():尽量避免使用,其无法控制线程数量, Schedu ...
- 论如何O(1)快速乘
然而并没有什么好论的... 直接贴代码算了... ll Mul(ll x,ll y,ll Mod){ x=(x%Mod+Mod)%Mod;y=(y%Mod+Mod)%Mod; return (x*y- ...
- javascript BOM基本知识
1.BOM(Bowser Object Model浏览器对象模型) 浏览器创建的对象通常称作文档(Document)对象,它是浏览器使用的众多对象的一部分,浏览器操作的对象结合起来称作浏览器对象模型( ...
- E20180124-hm
introspection n.自我反省; 反省,内省; capacity n. 容量; 性能; 才能; 生产能力; synthesize vt. 综合; 人工合成; (通过化学手段或生物过程) 合成 ...