题目链接: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自动机)的更多相关文章

  1. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  2. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  3. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  4. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  5. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  6. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  7. AC自动机 HDOJ 2222 Keywords Search

    题目链接 题意:每个文本串的出现次数 分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零.   新模板,加上last跑快一倍 #include <bits/stdc++.h> ...

  8. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  9. hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

随机推荐

  1. Javascript闭包概念剖析

    某种情况下,函数调用依然持有对其原始定义的作用域的引用,这个引用就叫做闭包. function foo(){ var a = 2; function bar(){ console.log(a); } ...

  2. Pull和SAX解析的区别

    Pull解析器和SAX解析器虽有区别但也有相似性.他们的区别为:SAX解析器的工作方式是自动将事件推入注册的事件处理器进行处理,因此你不能控制事件的处理主动结束:而Pull解析器的工作方式为允许你的应 ...

  3. Node.js 启动小结

    配置环境变量: cmd-------> 启动dyson服务 在项目的路径下 如open cmd进入open目录执行npm start命令 在浏览器下输入访问地址:http://localhost ...

  4. Web颜色搭配 - 收集

    颜色1  颜色一   背景 字 RGB 43,41,46 92,187,207 HEX #2B292E #5CBBCF HSB 264,11,18 190,56,81 CMYK 7,11,0,82 5 ...

  5. PhpStorm 10.0 激活方式

    随着 JetBrains 新版本的发布,注册机已然不行了.然而,道高一尺,魔高一丈.IntelliJ IDEA开源社区 提供了如下通用激活方法:注册时选择License server填写http:// ...

  6. 重拾javascript动态客户端网页脚本

    笔记一: 一.DOM 作用: ·              DOM(Doument Object Model) 1.document文档 HTML 文件 (标记语言) <html> < ...

  7. 转:CI伪静态化

    去掉php框架CI默认url中的index.php 2010-03-17 17:33:07|  分类: php框架ci |字号 订阅   CI默认的rewrite url中是类似这样的,例如你的CI根 ...

  8. DelphiXe5中的双向绑定(使用使用TBindScope和TBindExpression,并覆盖AfterConstruction函数)

    在Delphi下等这一功能很久了,虽然C#下早已实现了这一功能.但是在Dephi下尝试这项功能时还是有些许的激动.闲言少絮,直接上代码. unit BindingDemo; interface use ...

  9. gallery 从最左边开始显示并且默认选中第一个

    import android.content.Context; import android.graphics.Camera; import android.graphics.Matrix; impo ...

  10. svn中的Trunk,branches,tags深度理解

    trunk.就是主干,这个目录以下直接放源代码了,我们创建项目的时候,把项目源代码放到这个目录.import进svn branches.就是分支,以下可能有非常多trunk,比方trunk_1_0_1 ...