多模匹配

  题目大意:给定很多个字串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. Genymotion启动时出现错误virtualization engine not found

    打开VirtualBox,管理-全局设定,网络,仅主机“Host-only”网络,需要的设置如下

  2. CxImage

    启动项目的时候显示此时 百度“无法启动程序cximage.lib” 得到http://tieba.baidu.com/p/1935208210把第二项设为启动项即可 为什么设置第二项为启动项呢 因为h ...

  3. iOS:项目中疑难Crash问题集锦

    项目中疑难Crash问题集锦 iOS App运行中遇到Crash的情况相信大家都遇到过,开发和者测试中遇到了可能很方便的办法就是直接拿着设备连接一下,然后使用Xcode自带的工具就可以解析出Crash ...

  4. C#3.0 特性

    C#3.0特性 隐式类型的本地变量和数组 对象初始值设定项 集合初始值设定项 扩展方法 匿名类型 lambda表达式 查询关键字 自动实现的属性 分布方法定义 lambda表达式与表达式树 https ...

  5. Go - template 常用方法详解 及 注意事项

    Go template包下面有两个函数可以创建模板实例 func New(name string) *Template func ParseFiles(filenames ...string) (*T ...

  6. VMware 关闭虚拟机 Ubuntu 12 的 3D 效果,提高性能

    Ubuntu 2012,有 2D 和 3D 的渲染效果,但是 在虚拟机中,开启 3D 效果后,特别卡.好在 VMware 中有个“关闭3D”的开关,如下图所示: 去掉勾选“加速 3D 图形”

  7. 第三天 moyax

    struct Blog { static let BaseURL = NSURL(string: "http://192.168.1.103/blog")! } extension ...

  8. xcode7 The operation couldn't be completed.

    问题描述:当运行Xcode6时,编译代码成功,但是登陆模拟器失败,显示错误:The Operation couldn't be completed.(LaunchServicesError error ...

  9. MAS 移动业务整合系统

    AppCan MAS是基于高性能NODEJS架构开发的企业移动后端整合系统,内置各种标准协议组件,统一移动业务前后端标准开发技术:同时通过基于策略配置的数据缓存机制,聚合业务数据并发连接不同的后端业务 ...

  10. 类加载器ClassLoader之jar包隔离

    小引子 最近做了一个根据同一模块的不同jar版本做同时测试的工具,感觉挺有意思,特此记录. 类加载器(ClassLoader)是啥? 把类加载阶段中的"通过一个类的全限定名(博主注:绝对路径 ...