hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222
思路分析:该问题为多模式匹配问题,使用AC自动机解决;需要注意的问题是如何统计该待查询的字符串包含的关键字:
假设待查找的字符串为str[0..n],则str[i…j]可能为某一个关键字;假设当前正在匹配字符str[k],则以str[i..k]为关键字的所有可能
可能的关键字的最后一个字符为str[k],使用fail指针进行跳转并判断以str[k]结尾的该结点是否为关键字最后一个结点,重复进行该
操作直到回溯到根节点。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; const int KIND = ;
const int LEN_STR = + ;
const int MAX_N = + ; struct Node;
Node *q[MAX_N];
char insert_str[LEN_STR];
char str[MAX_N]; struct Node {
Node *fail;
Node *next[KIND];
int count; Node()
{
fail = NULL;
count = ;
memset(next, NULL, sizeof(next));
}
}; void Insert(char *str, Node *root)
{
Node *p = root;
int i = , index = ; while (str[i]) {
index = str[i++] - 'a';
if (p->next[index] == NULL)
p->next[index] = new Node();
p = p->next[index];
}
p->count++; // 单词的末尾会被标记为1
} void BuildAcAutomation(Node *root)
{
int head = , tail = ; root->fail = NULL;
q[tail++] = root;
while (head != tail) {
Node *temp = q[head++];
Node *p = NULL; for (int i = ; i < KIND; ++i) {
if (temp->next[i]) {
if (temp == root)
temp->next[i]->fail = root;
else {
p = temp->fail;
while (p != NULL) {
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 Query(char *str, Node *root)
{
int i = , cnt = , index = ;
Node *p = root; while (str[i]) {
index = str[i] - 'a';
while (!p->next[index] && p != root)
p = p->fail;
p = p->next[index];
p = (p == NULL) ? root : p; Node *temp = p;
while (temp != root && temp->count != -) {
cnt += temp->count;
temp->count = -;
temp = temp->fail;
}
++i;
}
return cnt;
} int main()
{
int case_times = ;
int ans = , n = ; scanf("%d", &case_times);
while (case_times--) {
Node *root = new Node();
scanf("%d", &n);
while (n--) {
scanf("%s", insert_str);
Insert(insert_str, root);
}
BuildAcAutomation(root); scanf("%s", &str);
ans = Query(str, root);
printf("%d\n", ans);
}
return ;
}
hdoj 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,那么当单词数量非 ...
- AC自动机 HDOJ 2222 Keywords Search
题目链接 题意:每个文本串的出现次数 分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零. 新模板,加上last跑快一倍 #include <bits/stdc++.h> ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- hbase 单机安装问题
报zookeeper exception not found I fixed this by editing the file "/usr/local/hbase-0.94.1/conf/h ...
- myeclipse破解教程,对所有版本有效,完美支持32位和64位
破解软件下载地址 执行Run.bat文件,按照以下步骤进行激活: 第一步:输入任意用户名 第二步:点击Systemid... 按钮,自动生成本机器的systemid. 第三步:点菜单Tools-> ...
- IIS发布网站:CS0016: 未能写入输出文件的解决方法
“/”应用程序中的服务器错误.-------------------------------------------------------------------------------- 编译错误 ...
- CSS3 Media Query
在移动端火爆的今日,一个好的web应用不仅仅要有对应移动平台的APP,自己web网页也需要对不同屏幕大小的移动设备进行支持,也就是我们所说的响应式web页面. 本篇文章就来介绍下最常见的响应式页面的实 ...
- 【原创】JPEG图像密写研究(三) 数据流译码
[原创]这次更新比较慢,译码过程比想象中复杂一些,更主要是译出来的DCT系数无法确定是否正确,要想验证就需要再进行正向压缩编码,再次形成jpeg图像验证正确,后续工作正在开展,这里就说一说译码的主要思 ...
- :last-child的诡异的问题!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- android小知识之注释模板(转载)
设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...
- 基于视觉信息的网页分块算法(VIPS) - yysdsyl的专栏 - 博客频道 - CSDN.NET
基于视觉信息的网页分块算法(VIPS) - yysdsyl的专栏 - 博客频道 - CSDN.NET 于视觉信息的网页分块算法(VIPS) 2012-07-29 15:22 1233人阅读 评论(1) ...
- hdu-Common Subsequence
http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description A subsequence of a given sequence ...
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...