HDU 2222 Keywords Search(AC自动机)题解
题意:给你几个keywords,再给你一段文章,问你keywords出现了几次。
思路:这里就要用到多模匹配算法AC自动机了,AC自动机需要KMP和字典树的知识,匹配时是在字典树上,失配我们就要用到类似KMP的失配值了,如果失配,我们就沿着失配值到某个节点开始匹配,因为是多模匹配,我们每次失配移动都会从某一keyword的某部分开始匹配,这样就节省了很多时间。
话说第一次听到AC自动机我竟天真的以为是会自动AC题目的算法...orz
参考:
代码:
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define ll long long
const int maxn = 1000000+5;
const int maxm = 100000+5;
const int MOD = 1e7;
const int INF = 0x3f3f3f3f;
using namespace std;
struct Trie{
Trie *next[26];
Trie *fail; //失配值
int sum; //以此为单词结尾的个数
Trie(){
sum = 0;
memset(next,NULL,sizeof(next));
fail = NULL;
}
};
Trie *root;
Trie *q[maxn]; //模拟队列
int head,tail;
void insert(char *s){
Trie *p = root;
for(int i = 0;s[i];i++){
int x = s[i] - 'a';
if(p ->next[x] == NULL){
p ->next[x] = new Trie();
}
p = p ->next[x];
}
p ->sum++;
}
void buildFail(){ //计算失配值
head = 0,tail = 1;
q[head] = root;
Trie *p,*temp;
while(head < tail){
temp = q[head++];
for(int i = 0;i <= 25;i++){
if(temp ->next[i]){
if(temp == root){ //父节点为root,fail为root
temp ->next[i] ->fail = root;
}
else{
p = temp ->fail; //查看父节点的fail
while(p){
if(p ->next[i]){
temp ->next[i] ->fail = p ->next[i];
break;
}
p = p ->fail;
}
if(p == NULL) temp ->next[i] ->fail = root;
}
q[tail++] = temp ->next[i];
}
}
}
}
int ac_automation(char *ch){
//p为模式串指针
int cnt = 0;
Trie *p = root;
int len = strlen(ch);
for(int i = 0;i < len;i++){
int x = ch[i] - 'a';
while(!p ->next[x] && p != root)
p = p ->fail;
p = p ->next[x]; //找到后p指针指向该结点
if(!p) p = root; //若指针返回为空,则没有找到与之匹配的字符
Trie *temp = p;
while(temp != root){
if(temp ->sum >= 0){ //判断该结点是否被访问
cnt += temp ->sum;
temp ->sum = -1;
}
else break;
temp = temp ->fail;
}
}
return cnt;
}
char word[maxn];
int main(){
int T,n;
char s[55];
scanf("%d",&T);
while(T--){
root = new Trie();
scanf("%d",&n);
for(int i = 0;i < n;i++){
scanf("%s",s);
insert(s);
}
scanf("%s",word);
buildFail();
int cnt = ac_automation(word);
printf("%d\n",cnt);
}
return 0;
}
HDU 2222 Keywords Search(AC自动机)题解的更多相关文章
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
随机推荐
- Redis单机主从高可用性优化
版权声明:本文由陈龙原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/127 来源:腾云阁 https://www.qclou ...
- ubuntu 14.04版本更改文件夹背景色为草绿色
ENV:ubuntu 14.04 在这个版本上使用dconf 工具无法改变文件夹的背景了,下面介绍其他的方法,不需要dconf工具. 第一步:在home目录下创建.themes文件夹 第二步将/usr ...
- WEB安全第四篇--与数据库的亲密接触:SQL注入攻击
零.前言 最近做专心web安全有一段时间了,但是目测后面的活会有些复杂,涉及到更多的中间件.底层安全.漏洞研究与安全建设等越来越复杂的东东,所以在这里想写一个系列关于web安全基础以及一些讨巧的pay ...
- C# MVC跳转
MVC方式: 显示提示框,并返回上一页 return Content("<script>alert('暂时没有实践作业!');history.go(-1);</script ...
- Oracle各种表空间
system表空间:含数据字典信息 sysaux表空间:存储各种oracle应用的元数据(如AWR的操作数据) 创建表空间后,不能改变区尺寸 区尺寸管理:自动分配(AUTOALLOCATE):小段64 ...
- vscode 中使用 csscomb
我看中 csscomb 主要是用来给 css 属性排序用的,当然他也有格式化的作用: 1.安装不必说,但是装了之后,默认似乎没用: 2.点开 文件-首选项,搜下 csscomb 就知道了: 3.第一个 ...
- 在centos上用nginx访问php显示404
yum install nginx -y 可以在浏览器 访问html文件,但是访问不了php文件 后来我看见别人别人响应的参数有php,而我没有我,就觉得php配置应该有问题: 我 yum insta ...
- HOJ 2139 Spiderman's workout(动态规划)
Spiderman's workout My Tags (Edit) Source : Nordic Collegiate Programming Contest 2003 Time limit : ...
- Oracle AWR之-enq: TX - allocate ITL entry
今天收到压力测试期间awr报告,测试人员要我看看数据库是否有可以优化的地方,数据库服务器配置信息:CPU:32*8,内存:480g 单实例数据库:oracle 11.2.0.4.具体分析过程如下: 可 ...
- Scala高级语法
一.隐式 implicit分类: (1)隐式参数 (2)隐式转换类型 (3)隐式类 特点:让代码变得更加灵活 (一)隐式参数 1.ImplicitTest object ImplicitTest { ...