多模匹配

  题目大意:给定很多个字串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. hdu5024 Wang Xifeng's Little Plot (水

    http://acm.hdu.edu.cn/showproblem.php?pid=5024 网络赛 Wang Xifeng's Little Plot Time Limit: 2000/1000 M ...

  2. 大熊君大话NodeJS之开篇------Why NodeJS(将Javascript进行到底)

    一,开篇分析 大家好啊,大熊君又来啦(*^__^*) 嘻嘻……,之前我写过一系列关于JS(OOP与设计模式)方面的文章,反响还好,其实这也是对我本人最大的鼓励,于是我决定我要将JavaScript进行 ...

  3. [译]Mongoose指南 - 查询

    查询有带callback和不带callback两种方式 所有mongoose的callback都是这种格式: callback(err, result) var Person = mongoose.m ...

  4. 文件操作 fopen() fclose()

    #define _CRT_SECURE_NO_DEPRECATE /*取消scanf,printf不安全之类的错误提示*/ /* fopen example */ #include <stdio ...

  5. CentOS系统操作mysql的常用命令

    MySQL名字的来历MySQL是一个小型关系型数据库管理系统,MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了 ...

  6. 必须知道的.net(字段、属性和方法)

    1.字段 通常定义为private(封装原则) 2.属性(property) 通常定义为public,表示类的对外成员.具有可读可写属性,通过get和set访问器实现 3.索引器(indexer) C ...

  7. 关于外部引用JS,中文乱码的问题

    asp.net 页面默认编码为UTF-8, 如果js嵌套写在asp.net中,不会导致中文乱码,因为他们具有相同的编码 外部引用js由于编码格式与asp.net的编码不同,javascript编码默认 ...

  8. 在WordPress后台菜单系统中添加Home链接

    在wordpress后台如果想打开前台的话,要想先把鼠标移动到左上角菜单,然后在下拉菜单中点击“查看站点”,很是麻烦,能不能在 WordPress 后台菜单系统中添加 Home 链接呢? 将下面代码复 ...

  9. Android在TextView中实现RichText风格

    参考: Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格 Demo: private SpannableStringBuilder c ...

  10. 在同一个页面使用多个不同的jQuery版本,让它们并存而不冲突

    - jQuery自诞生以来,版本越来越多,而且jQuery官网的新版本还在不断的更新和发布中,现已经达到了1.6.4版本,但是我们在以前的项目中就已经使用了旧版本的jQuery,比如已经出现的:1.3 ...