HD2222 Keywords Search(AC自动机入门题)
然而还不是很懂=_=
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int Max = +;
char str[Max];
struct node
{
int cnt;
struct node * Next[];
struct node * fail;
void init()
{
for (int i = ; i < ; i++)
Next[i] = NULL;
cnt = ;
fail = NULL;
}
};
node * root;
void Insert()
{
node * p = root;
int len = strlen(str);
for (int i = ; i < len; i++)
{
int id = str[i] - 'a';
if (p->Next[id] == NULL)
{
p->Next[id] = new node();
p->Next[id]->init();
}
p = p->Next[id];
}
p->cnt++;
}
void getFail()
{
node * p = root, *temp, *son;
queue<struct node * > que;
que.push(p);
while (!que.empty())
{
temp = que.front();
que.pop();
for (int i = ; i < ; i++)
{
son = temp->Next[i];
if (son != NULL)
{
if (temp == root)
{
son->fail = root;
}
else
{
p = temp->fail;
while (p)
{
if (p->Next[i])
{
son->fail = p->Next[i];
break;
}
p = p->fail;
}
if (!p)
son->fail = root;
}
que.push(son);
}
}
}
}
void querry()
{
int len, cnt = ;
len = strlen(str);
node * p, * temp;
p = root;
for (int i = ; i < len; i++)
{
int pos = str[i] - 'a';
while (!p->Next[pos] && p != root)
p = p->fail;
p = p->Next[pos];
if (!p)
p = root;
temp = p;
while (temp != root)
{
if (temp->cnt >= )
{
cnt += temp->cnt;
temp->cnt = -;
}
else
break;
temp = temp->fail;
}
}
printf("%d\n", cnt);
}
int main()
{
int test, n;
scanf("%d", &test);
while (test--)
{
root = new node();
root->init();
root->fail = NULL;
scanf("%d", &n);
getchar();
for (int i = ; i < n; i++)
{
gets(str);
Insert();
}
getFail();
gets(str);
querry();
}
return ;
}
HD2222 Keywords Search(AC自动机入门题)的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- SqlServer--用代码创建和删除数据库或表
创建数据库,创建表,设置主键数据库的分离和附加MS SQLServer的每个数据库包含:1个主数据文件(.mdf)必须.1个事务日志文件(.ldf)必须.可以包含:任意多个次要数据文件(.ndf)多个 ...
- SQL Server 2008 R2 升级到 Service Pack 3后Report Builder启动不了
一同事将测试服务器从SQL Server 2008 R2 SP2升级到了SQL Server 2008 R2 SP3后发现Report Service的报表编辑时启动不了Report Builder, ...
- apache+mysql+php的环境配置
一 配置前的准备 1 先设置环境变量(win7的) win10 二 配置apache 我用EditPlus打开httpd.conf LoadModule php5_module "c:/w ...
- Python爬虫代理池
爬虫代理IP池 在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代理IP,从而保证爬虫快速稳定的运行,当然在公司做的东西不能开源出来 ...
- Neutron 理解 (9): OpenStack 是如何实现 Neutron 网络 和 Nova虚机 防火墙的 [How Nova Implements Security Group and How Neutron Implements Virtual Firewall]
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- C#学习笔记-KeyDown、KeyPress、KeyUp事件以及KeyCode、KeyData、KeyValue、KeyChar属性
本来没打算单独写的,但是在自己弄测试小程序的时候,越写发现不清楚的东西越多,所以实践又一次证明:纸上得来终觉浅,绝知此事要躬行! 直接贴代码了: //发生顺序:KeyDown->KeyPress ...
- Makefile 编写 tips
1.变量赋值 VARIABLE = value #在执行时扩展,允许递归扩展 VARIABLE := value #在定义时扩展 VARIABLE ?= value #只有在该变量为空时才设置该值 V ...
- 【转载】Serif和Sans-serif字体的区别
在西方国家罗马字母阵营中,字体分为两大种类:Sans Serif和Serif,打字机体虽然也属于Sans Serif,但由于是等宽字体,所以另外独立出Monospace这一种类,例如在Web中,表示代 ...
- 第5章 Java数组
1.什么是数组 数组可以想象成一个巨大的盒子,这个盒子里面存放的是同一个数据类型的数据 例如:int[] scores = {78,68,94,93}; 2.如何使用Java中的数组 2.1申明数组 ...
- python_爬虫一之爬取糗事百科上的段子
目标 抓取糗事百科上的段子 实现每按一次回车显示一个段子 输入想要看的页数,按 'Q' 或者 'q' 退出 实现思路 目标网址:糗事百科 使用requests抓取页面 requests官方教程 使用 ...