<题目链接>

题目大意:

给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词。

解题分析:

这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非常多的时候,耗时会非常多,所以这里用到了AC自动机,这是一种类似于Trie树的数据结构,但是同时,它也用到了KMP算法中 next数组的思想。

本题可做模板:

#include <bits/stdc++.h>
using namespace std; const int N = 5e5+;
int nxt[N][],cnt[N],fail[N],pos; inline void insert(char *s){
int now=;
for(int i=;s[i];++i){
int to=s[i]-'a';
if(!nxt[now][to])nxt[now][to]=++pos;
now=nxt[now][to];
}
++cnt[now];
}
/*构造失败指针的过程概括起来就一句话:设这个节点上的字母为C,沿着他父亲的失败指针走,直到走到一个节点,他的儿
子中也有字母为C的节点。然后把当前节点的失败指针指向那个字母也为C的儿子。如果一直走到了root都没找到,那就把失败
指针指向root。具体操作起来只需要:先把root加入队列(root的失败指针指向自己或者NULL),这以后我们每处理一个
点,就把它的所有儿子加入队列*/
inline void getFail(){
memset(fail,,sizeof(fail)); //fail指针初始化全部指向根节点
queue<int>q;
for(int i=;i<;++i)
if(nxt[][i])q.push(nxt[][i]);
while(!q.empty()){
int now=q.front();q.pop();
for(int i=;i<;++i){
if(nxt[now][i])q.push(nxt[now][i]),fail[nxt[now][i]]=nxt[fail[now]][i]; //下一个元素的fail指针指向当前元素fail指针指向的元素对应的下一个元素
else nxt[now][i]=nxt[fail[now]][i];
}
}
}
inline int query(char *s){
int now=,res=;
for(int i=;s[i];i++){
int tmp=nxt[now][s[i]-'a'];
while(tmp){
if(cnt[tmp]>=){
res+=cnt[tmp];
cnt[tmp]=-;
}else break;
tmp=fail[tmp];
}
now=nxt[now][s[i]-'a'];
}
return res;
}
char str[int(1e6+)],s[];
int main(){
int T;cin>>T;
while(T--){
pos=;
memset(nxt,,sizeof(nxt));
memset(cnt,,sizeof(cnt));
int n;scanf("%d",&n);
while(n--){
scanf("%s",s);
insert(s); //将模式串插入trie图中
}
getFail();
scanf("%s",str);
printf("%d\n",query(str));
}
}

HDU 2222 Keywords Search (AC自动机)(模板题)的更多相关文章

  1. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

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

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

  3. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  4. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  5. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  6. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

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

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

  8. POJ2222 Keywords Search AC自动机模板

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

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

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

随机推荐

  1. CF418D Big Problems for Organizers

    传送门 题意,给一棵树,每次给两个点\(x,y\),求\(\max_{i=1}^{n}(\min(di_{x,i},di_{y,i}))\) 看std看了好久 以下是一个优秀的在线做法,\(O(nlo ...

  2. php 全局变量问题

    当在函数里通过require_once包含另外php文件. 而另外php文件包含了另外php文件,而该php文件的函数需要另外的php文件. 例子: installment_maintenance_s ...

  3. Informatic学习总结_day03_update组件学习

  4. Debian 无线网卡驱动问题

    参考这里:https://wiki.debian.org/iwlwifi Debian 9 "Stretch" Add a "non-free" compone ...

  5. ARMV8 datasheet学习笔记4:AArch64系统级体系结构之Generic timer

    1.前言 2.generate timer 2.1 概述 提供了一个系统计数器,用来实时测量流逝的时间: 提供了一个虚拟计数器,用来测量某个虚拟机上流逝的虚拟时间: 定时器,每隔一段时间会触发事件,支 ...

  6. Linux下rsyslog日志收集服务环境部署记录【转】

    rsyslog 可以理解为多线程增强版的syslog. 在syslog的基础上扩展了很多其他功能,如数据库支持(MySQL.PostgreSQL.Oracle等).日志内容筛选.定义日志格式模板等.目 ...

  7. springboot系列一、springboot产生背景及介绍

    一.为什么用Springboot 长期以来 Java 的开发一直让人所诟病: ·Java 项目开发复杂度极其高: · Java 项目的维护非常困难: · 在云时代如何实现项目的快速部署以及快速启动: ...

  8. 创建虚拟机时,提示No valid host was found解决办法

    1.http://blog.csdn.net/yxwmzouzou/article/details/43892261 2.http://www.cnblogs.com/kevingrace/p/601 ...

  9. TCP/IP指纹鉴别 fingerprint

    http://www.freebuf.com/articles/system/30037.html使用TCP/IP协议栈指纹进行远程操作系统辨识 Fyodor <fyodor@insecure. ...

  10. centos6.5生产环境编译安装nginx-1.11.3并增加第三方模块ngx_cache_purge、nginx_upstream_check、ngx_devel_kit、lua-nginx

    1.安装依赖包 yum install -y gcc gcc-c++ pcre-devel openssl-devel geoip-devel 2.下载需要的安装包 LuaJIT-2.0.4.zip ...