HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接>
题目大意:
给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词。
解题分析:
这是多模匹配问题,如果用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自动机)(模板题)的更多相关文章
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- SRS服务器搭建,ffmpeg 本地推流,srs从本地拉流
参考: https://github.com/ossrs/srs/wiki/v2_CN_SampleFFMPEG git clone https://github.com/ossrs/srs cd s ...
- 矩阵的SVD分解
转自 http://blog.csdn.net/zhongkejingwang/article/details/43053513(实在受不了CSDN的广告) 在网上看到有很多文章介绍SVD的,讲的也都 ...
- Linux时间日期类指令
⒈date [Options] 显示/设置当前日期 基本语法: date 显示当前时间 date +"%Y" 显示当前年份 date +"%m" 显示当前月份 ...
- CONFIG_DEBUG_USER【转】
转自:https://blog.csdn.net/adaptiver/article/details/12778621 关于CONFIG_DEBUG_USER 把menuconfig中查到的 CONF ...
- 【转】如何安装JDK以及配置Java运行环境
具体的参考这篇博文就好了~~!http://www.cnblogs.com/liu-en-ci/p/6743106.html
- windows下配置mysql数据库监视工具Mysqlreport
该工具除了可以监控本机Mysql数据库外,也可以监控远程服务器mysql数据库 需要的工具: 1:perl脚本解析工具安装: http://www.activestate.com/activeperl ...
- mysql语句判断是否存在记录,没有则插入新纪录否则不执行
1 前言 由于项目需要,当某个表如果有记录,就不执行加入语句,否则加入新纪录(测试数据).思路是:判断表的记录是否为空,然后再决定是否插入 2 代码 DROP PROCEDURE IF EXISTS ...
- python读取两个csv文件数据,进行查找匹配出现次数
现有需求 表1 表2 需要拿表1中的编码去表2中的门票编码列匹配,统计出现的次数,由于表2编码列是区域间,而且列不是固定的,代码如下 #encoding:utf-8 ##导入两个CSV进行比对 imp ...
- ExtJs常用布局--layout详解(含实例)
序言: 笔者用的ExtJs版本:ext-3.2.0 ExtJs常见的布局方式有:border.form.absolute.column.accordion.table.fit.card.anchor ...
- keras2.0的一些变化
keras 变化太快了https://github.com/fchollet/keras/wiki/Keras-2.0-release-notes