题目链接: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. 经典SQL语句集锦

      下列语句部分是MsSql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELET ...

  2. SDOI2008 Sandy的卡片( 后缀数组 )

    求出后缀数组, 然后二分答案, 对height数组分组检验答案. 时间复杂度O(|S| log|S|) ------------------------------------------------ ...

  3. BZOJ 2946: [Poi2000]公共串( 后缀自动机 )

    一个串建后缀自动机, 其他串在上面跑, 然后用当前串跑的去更新全部 ------------------------------------------------------------------ ...

  4. mysql 正确的关闭方式

    ./bin/mysqladmin -uroot -p123456 -S /home/allen/var/mysql/mysql.sock shutdown

  5. 列求key出现的频率

    1 cat mc.log | grep LOGIN_GET | awk '{print $9}' | sort | uniq -c

  6. 常用Json

    一般Json是页面与页面之间传递使用. Json用途        1 后台与前台数据交互,并且数据较复杂,如果数据单一,直接传递字符串,然后在前台用js分割就行. 2 webservice和html ...

  7. Nginx启动报错:10013: An attempt was made to access a socket in a way forbidden

    Nginx在win7,win2008下启动报错:bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket i ...

  8. jQuery数据缓存data(name, value)详解及实现

    一. jQuery数据缓存的作用 jQuery数据缓存的作用在中文API中是这样描述的:“用于在一个元素上存取数据而避免了循环引用的风险”.如何理解这句话呢,看看我下面的举例,不知道合不合适,如果你有 ...

  9. VS中,NUnit适合测试者尽心开发自动化测试,而Unit适合开发者开发单元测试。

    1.整合Visual Studio和NUnit 在Visual Studio 2010中,通过安装NUnit插件,可以不使用外部客户端,直接运行测试. 当然,貌似在最新版本的VS2012中,安装过NU ...

  10. 一些特殊css

    属性 描述            outline  (轮廓)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用. outline:#00FF00 dotted thick; 可以按顺序 ...