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 ...
随机推荐
- 浅析HTTPS与SSL原理
版权声明:本文由盛旷 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/134 来源:腾云阁 https://www.qclo ...
- TCP异常关闭研究分析
版权声明:本文由谢代斌原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/108 来源:腾云阁 https://www.qclo ...
- Android 跨进程调用忽略权限
Framework层: @Override public StackInfo getStackInfo(int stackId) { final int callingUid = ...
- 2800 送外卖[状态压缩dp]
2800 送外卖 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 有一个送外卖的,他手上有n份订单,他 ...
- centos7安装mysql(MariaDB)
1.centos7现状: 新系统无法再使用yum install mysql-server来安装mysql,因为已使用mariadb代替mysql. 2.安装mariadb: [root@localh ...
- [MongoDB] 机器换IP之后的设置
组里用的几台机器换了网段,MongoDB需要做重新配置. 查看Replica Set的状态如下: rs.status() { "startupStatus" : 1, " ...
- centos6安装postgresql-(2)
1.Install yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-ce ...
- treeview(树加载)
数据库结构 id:int类型,主键,自增列: Name:char类型: paraid:int类型 窗台拖入控件treeview. 1.版本1 using System; using S ...
- Eclipse Tomcat插件的配置, 及 Tomcat 的配置
Eclipse Tomcat插件的配置, 及 Tomcat 的配置 首先下载 对应 eclipse 版本的 tomcat 插件版本,(这里要注意: Tomcat 插件是Tomcat 插件,Tomc ...
- Scala面向对象和模式匹配
我们要封装数据,定义模板等操作,所以我们需要面向对象. 一.scala中的单例对象 在scala当中,是没有static的,scala给我们提供了单例模式的实现方法.就是使用关键字object. st ...