Match:Keywords Search(AC自动机模板)(HDU 2222)

题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串。
经典AC自动机模板题,不多说。
#include <iostream>
#include <algorithm>
#include <functional>
#include <string.h>
#define MAX 26 using namespace std; struct node
{
node *fail, *next[MAX];
int count;
}Mem_Pool[], *Trie_Queue[];
static int Used_Node;
static char pattern[], target[]; struct node *Get_New_Node(void);
void Insert(struct node*);
void Build_AC_Automation(struct node* const);
static int Query(struct node* const); int main(void)
{
int case_sum, pattern_sum;
scanf("%d", &case_sum); while (case_sum--)
{
Used_Node = ;
node *root = Get_New_Node();//每一次都拿一个新的根
scanf("%d", &pattern_sum);
getchar();
while (pattern_sum--)
Insert(root);
Build_AC_Automation(root);
gets(target);
printf("%d\n", Query(root));
}
} struct node *Get_New_Node(void)
{
Mem_Pool[Used_Node].count = ;
Mem_Pool[Used_Node].fail = NULL;
memset(Mem_Pool[Used_Node].next, , sizeof(struct node *)*MAX); return &Mem_Pool[Used_Node++];
} void Insert(struct node *root)
{
gets(pattern); node *p = root;
int index;
for (int i = ; pattern[i] != '\0'; i++)
{
index = pattern[i] - 'a';
if (p->next[index] == NULL)
p->next[index] = Get_New_Node();
p = p->next[index];
}
p->count++;//表示这个位置包含着一个字符,如果有多个则叠加就可以了。
} void Build_AC_Automation(struct node *const root)//自动机的构造例程,其实就是个BFS
{
root->fail = NULL;
int head = , back = ;
node *out = NULL, *tmp = NULL;
Trie_Queue[back++] = root; while (head != back)
{
out = Trie_Queue[head++];
for (int i = ; i < MAX; i++)//枚举所有情况
if (out->next[i] != NULL)
{
if (out == root)
out->next[i]->fail = root;//如果out自己就是根,那么直接将其子节点的失败指针指向自己就好了
else
{
for (tmp = out->fail; tmp != NULL; tmp = tmp->fail)
if (tmp->next[i] != NULL)
{
out->next[i]->fail = tmp->next[i];
//如果还找到在其他地方找到和他一样的元素,那么我们就把失败指针指向这个元素
break;
}
if (tmp == NULL)
out->next[i]->fail = root;
}
Trie_Queue[back++] = out->next[i];
}
}
} static int Query(struct node *const root)
{
int ans = ;
node *tmp = root, *other = NULL;
//tmp是跟着目标串的移动而移动的,other指针则表示在tire树的其他位置看还能不能匹配到其他字串
for (int i = ; target[i] != '\0'; i++)
{
int index = target[i] - 'a';
while (tmp->next[index] == NULL && tmp != root)
tmp = tmp->fail;//沿着失败指针一直走
tmp = tmp->next[index];
tmp = (tmp == NULL) ? root : tmp;//检查如果是已经是root了,直接跳出来就好了
for (other = tmp; other != root && other->count != -; other = other->fail)
{
ans += other->count;
other->count = -;//标记一下,不要重复扫描
}
}
return ans;
}

Match:Keywords Search(AC自动机模板)(HDU 2222)的更多相关文章
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
随机推荐
- java.lang.Class
java.lang.Class 一.Class类是什么 Class是一个类,位于java.lang包下. 在Java中每个类都有一个相对应的Class类的对象,换句话说:Java程序在启动运行时 一个 ...
- JAVA常用时间操作类
http://www.360doc.com/content/10/1210/17/2703996_76839640.shtml 在程序里面要获取当前的开始时间和结束时间,以及当前天所在的周的开始 ...
- 【转载】Ubuntu 系列安装 Docker
系统要求 Docker 支持以下版本的Ubuntu操作系统: Ubuntu Xenial 16.04 (LTS) Ubuntu Wily 15.10 Ubuntu Trusty 14.04 (LTS) ...
- Apache中,同一IP使用多域名对应多个网站的方法
首先dns中确定有相应的A记录, abc IN A 211.154.2.5 mail IN A 211.154.2.5 这个讲的是在windows下面配置apache虚拟主机: 一.配置虚拟 ...
- 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛
题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...
- Hibernate之映射一对一关联
一.一对一关联的概念: 一对一之间的关联是指:两张表中的信息是一对一的关系,比如我们每个人和身份证的关系,一个人对应一张身份证,一张身份证也只能对应一个人. Hibernate提供了两种映射一对一关联 ...
- Xcode 7 App Transport Security has blocked a cleartext HTTP 报错解决办法
Xcode 7 创建新项目用到 UIWebView 发送请求时,报下面的错: “App Transport Security has blocked a cleartext HTTP (http:// ...
- iOS开发——多线程篇——快速生成沙盒目录的路径,多图片下载的原理、SDWebImage框架的简单介绍
一.快速生成沙盒目录的路径 沙盒目录的各个文件夹功能 - Documents - 需要保存由"应用程序本身"产生的文件或者数据,例如:游戏进度.涂鸦软件的绘图 - 目录中的文件会被 ...
- 小米手机无法打开程序报错Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication的解决办法
打开studio的setting 然后 Preferences -> Build, Execution, Deployment -> Instant Run -> Enable In ...
- CSS继承总结
CSS的一个重要特征就是继承,它是依赖于祖先-后代的关系的.继承是一种机制,它允许样式不仅可以应用于某个特定的元素,还可以应用于它的后代. CSS可以继承的属性有: 1.文字相关:font-famil ...