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 ...
随机推荐
- 由JS函数返回值引发的一场”血案"
---恢复内容开始--- 啊... 本来昨天晚上想写来着,结果陪老婆看电视剧就忘了... 呢滴神啊,原谅我吧. 背景:昨天在项目中做一个小功能的时候,出现了个小问题,而且一开始找了半天也没找到原因. ...
- 自定义事件类EventManager (TS中...args的使用例子)
一个自定义事件类 初衷是使用Egret的事件有两点比较麻烦 1 在事件处理函数时,需要从e中获取data hander(e:egret.Event){ let data = e.data; } 2 ...
- nexus使用方法
转自:https://www.cnblogs.com/grey-wolf/p/6481166.html centos7搭建nexus maven私服 前置条件: 1.安装jdk,可参考 http: ...
- squid 高匿设置
1.透明代理.匿名代理.混淆代理.高匿代理有什么区别 这4种代理,主要是在代理服务器端的配置不同,导致其向目标地址发送请求时,REMOTE_ADDR, HTTP_VIA,HTTP_X_FORWARDE ...
- AVG
AVG([ DISTINCT | ALL ] expr) [ OVER(analytic_clause) ] SELECT MANAGER_ID, LAST_NAME, ...
- 03.JMS深入
1.JMS API JMS API可以分为3个主要部分:公共API.点对点API和发布/订阅API.在JMS1.1中,公共API可被用于向一个队列或主题发送消息,或从其中接收消息.点对点API ...
- 使用 Capistrano 进行自动化部署
最近在折腾这个,弄了好多次都不成功,看了官方文档和很多博客,都没有说清楚,因此,我觉得有必要把它记录下来,以帮助更多像我这样被弄得烦躁的人. 首先是安装,其实 Ubuntu 上面安装 Capistra ...
- php中函数preg_match或preg_match_all 第三个参数$match的解释
理解自:http://www.cnblogs.com/vicenteforever/articles/1623137.html php手册中是这样解释的 matches 如果提供了参数matches, ...
- Django权限系统auth模块详解
转自:原文出处 auth模块是Django提供的标准权限管理系统,可以提供用户身份认证, 用户组和权限管理. auth可以和admin模块配合使用, 快速建立网站的管理系统. 在INSTALLED_A ...
- 解决li中文字行高对齐
<div class="A_content_1"> <li>座右铭: <input class="A_ct1" type=&quo ...