多模匹配

  题目大意:给定很多个字串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)的更多相关文章

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

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

  2. Keywords Search(AC自动机模板)

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

  3. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

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

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

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

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

  6. HDU2222 Keywords Search [AC自动机模板]

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

  7. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

  8. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

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

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

随机推荐

  1. TeX — Beauty and Fun

    我是初学者,你推荐使用什么发行的 TeX? 我应该用 LaTeX 吗? 我认为最好的发行是 TeXLive CD,它不但包含了所有操作系统需要的程序,而且有许许多多宏包,如果你不是特别特殊的用户,有了 ...

  2. LAMP环境CentOS6.4 PHP5.4随笔未整理

    首先安装一些辅助的软件或者说是依赖的关系包. 1.安装libxml2: libxml是一个用来解析XML文档的函数库.它用C语言写成, 并且能为多种语言所调用,例如C语言,C++,XSH.C#, Py ...

  3. 基础知识系列☞IList ←vs→ List

    原文地址→http://www.cnblogs.com/zbphot/archive/2011/11/04/2235933.html IList接口→表示可按照索引单独访问的对象的非泛型集合. ILi ...

  4. 【Solr】copy字段的应用

    目录 界面查询应用 熟悉Schema.xml copy域的应用 回到顶部 界面查询应用 添加一个文档 查询添加的文档 以上详细介绍了query里面的参数详解. 当不输入任何条件时,进行查询,看看返回结 ...

  5. AlwaysOn可用性组功能测试(二)--SQL Server群集故障转移对AlwaysOn可用性组的影响

    三. SQL Server群集故障转移对AlwaysOn可用性组的影响 1. 主副本在SQL Server群集CLUSTEST03/CLUSTEST03上 1.1将节点转移Server02.以下是故障 ...

  6. PHP简易聊天室&调试问题

    在进入login.php程序之后 <?php error_reporting(E_ALL^E_NOTICE); session_start();  //装载Session库,一定要放在首行 $u ...

  7. 微信小程序即将开放申请?微信小论坛小程序专场16日或可见分晓

    9月22号微信小程序内测至今已经好一段时间了,首批只开放了200个名额,没拿到内测资格的朋友早就等到心急了.就在刚刚,微信公开课宣布微信小论坛小程序专场即将在11月16号举行,微信公众平台小程序会在当 ...

  8. 线程7-ThreadLocal

    有时间再整理 ThreadLocal不是用来解决对象共享访问问题的,而主要是提供了保持对象的方法和避免参数传递的方便的对象访问方式.归纳了两点: 1.每个线程中都有一个自己的ThreadLocalMa ...

  9. ubutu安装搜狗

    1.下载deb文件 下载32位 wget "http://pinyin.sogou.com/linux/download.php?f=linux&bit=32" -O &q ...

  10. zabbix 3.0.4 监控windows 服务

    下载客户端 http://www.zabbix.com/download.php http://www.zabbix.com/downloads/3.0.4/zabbix_agents_3.0.4.w ...