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 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- .NET Framework各版本比较
(1)本文比较了.NET Framework多个版本之间的区别,方便各位选择和切换.NET Framework. 版本号 发布日期 Visual Studio的版本 Windows上的默认情况 CLR ...
- winform窗体(一)——基本属性
一.窗体设计界面 二.部分属性 1.基本 设计中的Name:窗体类的类名 AcceptButton:窗口的确定按钮Enter CancelButton:窗口按ESC的取消按钮 2.外观 Backcol ...
- YARN与MRv1的对比
YARN与MRv1的对比 转载请注明出处:http://www.cnblogs.com/BYRans/ Hadoop 1.0存在的问题 由于Hadoop 1.0的良好特性,Hadoop 1.0被应用到 ...
- JavaScript中知而不全的this
都说 JavaScript 是一种很灵活的语言,这其实也可以说它是一个混乱的语言.它把 函数式编程和 面向对象编程糅合一起,再加上 动态语言特性,简直强大无比(其实是不能和C++比的,^_^ ). 这 ...
- JAVA防盗链在报表中的应用实例
今天我们来聊聊Java防盗链,多说无用,直接上应用案例. 这里所用的工具是报表软件FineReport,搭配有决策系统(一个web前端展示系统,主要用于权限控制),可以采用java防盗链的方式来实现页 ...
- (转)String、StringBuffer与StringBuilder之间区别
原文地址: http://www.cnblogs.com/A_ming/archive/2010/04/13/1711395.html 关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺 ...
- css3属性-webkit-font-smoothing
对字体进行抗锯齿渲染可以使字体看起来会更清晰舒服.在图标字体成为一种趋势的今天,抗锯齿渲染使用也越来越多. font-smoothing是非标准的CSS定义.它被列入标准规范的草案中,后由于某些原因从 ...
- Java的String中的subString()方法
方法如下: public String substring(int beginIndex, int endIndex) 第一个int为开始的索引,对应String数字中的开始位置, 第二个是截止的索引 ...
- Flooded!
Flooded! Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5955 Accepted: 1800 Specia ...
- Spring事务管理----声明式:利用TransactionProxyFactoryBean生成事务代理
通常建议采用声明式事务管理.声明式事务管理的优势非常明显:代码中无需关于关注事务逻辑,让spring声明式事务管理负责事务逻辑,声明式事务管理无需与具体的事务逻辑耦合,可以方便地在不同事务逻辑之间切换 ...