题目链接: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. 最新版SDWebImage的使用

    我之前写过一篇博客,介绍缓存处理的三种方式,其中最难,最麻烦,最占内存资源的还是图片缓存,最近做的项目有大量的图片处理,还是采用了SDWebImage来处理,但是发现之前封装好的代码报错了.研究发现, ...

  2. PHP系列笔记——Zend_Controller工作流程

    Zend_Controller_Front接收请求,然后调用Zend_Controller_Router_Rewrite来决定哪个控制器被派遣.为了在请求中设置控制器和动作名称,Zend_Contro ...

  3. nginx args

    $args $content_length $content_type $document_root $document_uri $host $http_user_agent $http_cookie ...

  4. tomcat无法正常启动的一个原因

    简要报错信息: java.lang.IllegalArgumentException: Document base E:\apache-tomcat-7.0.65\webapps\springmvc0 ...

  5. ButterKnife使用小结

    项目官网:http://jakewharton.github.io/butterknife/ Github主页:https://github.com/JakeWharton/butterknife 这 ...

  6. npm note

    npm docs 设置镜像站 因为npmjs的官方网站,总会下载比较慢或打不开,所以通常需要设置一下镜像站来更好的安装npm库 npm install --registry http://regist ...

  7. php执行shell更新svn文件的方法

    vim /etc/sudoers 修改内容如下: #Defaults !visiblepw Defaults visiblepw #Defaults requiretty <?php set_t ...

  8. android下tcpdump抓包

    tcpdump是最快捷方便的抓包方式,还可以加深对网络协议的理解.android下可以通过如下方式抓包: 1 Android上启动tcpdump Android设备可以把tcpdump的可执行文件上传 ...

  9. R使用入门

      R是一个开源的统计学软件包,用于数据计算,绘图等等用途,看介绍与大数据走得比较近. 入门还是很简单的,安装文件也非常的小. 官网网站,下载对应系统的安装包,55M,比matlab小多了,像操作系统 ...

  10. wndows make images

    配置文件/etc/xen/mywindows.内容如下 import os, re arch_libdir = 'lib' arch = os.uname()[4] if os.uname()[0] ...