hdu 1277 AC自动机
全文检索
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1134 Accepted Submission(s): 357
问题的描述是这样的:给定一个信息流文件,信息完全有数字组成,数字个数不超过60000个,但也不少于60个;再给定一个关键字集合,其中关键字个数不超过10000个,每个关键字的信息数字不超过60个,但也不少于5个;两个不同的关键字的前4个数字是不相同的;由于流文件太长,已经把它分成多行;请你编写一个程序检索出有那些关键字在文件中出现过。
646371829920732613433350295911348731863560763634906583816269
637943246892596447991938395877747771811648872332524287543417
420073458038799863383943942530626367011418831418830378814827
679789991249141417051280978492595526784382732523080941390128
848936060512743730770176538411912533308591624872304820548423
057714962038959390276719431970894771269272915078424294911604
285668850536322870175463184619212279227080486085232196545993
274120348544992476883699966392847818898765000210113407285843
826588950728649155284642040381621412034311030525211673826615
398392584951483398200573382259746978916038978673319211750951
759887080899375947416778162964542298155439321112519055818097
642777682095251801728347934613082147096788006630252328830397
651057159088107635467760822355648170303701893489665828841446
069075452303785944262412169703756833446978261465128188378490
310770144518810438159567647733036073099159346768788307780542
503526691711872185060586699672220882332373316019934540754940
773329948050821544112511169610221737386427076709247489217919
035158663949436676762790541915664544880091332011868983231199
331629190771638894322709719381139120258155869538381417179544
000361739177065479939154438487026200359760114591903421347697
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; typedef struct node
{
int id;
node *fail;
node *next[];
node()
{
id = ;
fail = NULL;
memset(next,NULL, sizeof(next));
}
}TreeNode; int res[];
int nCount = ; void Insert(TreeNode *pRoot, char Substr[], int id)
{
int nLen = strlen(Substr);
TreeNode *p = pRoot;
for (int i = ; i < nLen; i++)
{
int index = Substr[i] - '';
if (p->next[index] == NULL)
{
p->next[index] = new TreeNode;
}
p = p->next[index];
}
p->id = id;
} void getFail(TreeNode *pRoot)
{
queue<TreeNode*> Queue;
Queue.push(pRoot);
while(!Queue.empty())
{
TreeNode *p = Queue.front();
Queue.pop();
for (int i = ; i < ; i++)
{
if (p->next[i] != NULL)
{
if (p == pRoot)
{
p->next[i]->fail = pRoot;
}
else
{
TreeNode *temp = p->fail;
while(temp != NULL)
{
if (temp->next[i] != NULL)
{
p->next[i]->fail = temp->next[i];
break;
}
temp = temp->fail;
}
if (temp == NULL)
{
p->next[i]->fail = pRoot;
}
}
Queue.push(p->next[i]);
}
}
}
} void find(TreeNode *pRoot, char str[])
{
TreeNode *p = pRoot;
int nLen = strlen(str);
for (int i = ; i < nLen; i++)
{
int index = str[i] - '';
while(p != pRoot && p->next[index] == NULL)
{
p = p->fail;
}
p = p->next[index];
if (p == NULL)
{
p = pRoot;
}
TreeNode *temp = p;
while(temp != pRoot)
{
if (temp->id > )
{
res[nCount++] = temp->id;
temp->id = ;//同一个关键字只用一次
}
temp = temp->fail;
}
}
} void DeleteNode(TreeNode *pRoot)
{
for (int i = ; i < ; i++)
{
if (pRoot != NULL)
{
DeleteNode(pRoot->next[i]);
}
}
delete pRoot;
} int main()
{
int i,m, n,c;
char temp[];
char str[];
while(~scanf("%d%d", &m, &n))
{
strcpy(str,"");
TreeNode *pRoot = new TreeNode;
for (i = ; i < m; i++)
{
scanf("%s", temp);
strcat(str, temp);
}
getchar();
for (i = ; i < n; i++)
{
getchar();
scanf("[Key No. %d] %s",&c,temp);
Insert(pRoot, temp, i + );
}
getFail(pRoot);
find(pRoot, str);
if (nCount)
{
printf("Found key:");
for (i = ; i < nCount; i++)
{
printf(" [Key No. %d]", res[i]);
}
printf("\n");
}
else
{
printf( "No key can be found !\n" );
}
// DeleteNode(pRoot);
}
return ;
}
hdu 1277 AC自动机的更多相关文章
- hdu 1277 AC自动机入门(指针版和数组版)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...
- hdu 2896 AC自动机
// hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...
- hdu 3065 AC自动机
// hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...
- hdu 5880 AC自动机
Family View Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 3065 AC自动机(各子串出现的次数)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 5384 AC自动机
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:给n个母串,给m个匹配串,求每个母串依次和匹配串匹配,能得到的数目和. 分析:之前并不知道AC ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
随机推荐
- SCOPE_IDENTITY和@@IDENTITY[转]
本文转自:http://www.cnblogs.com/daydayupanan/archive/2008/09/04/1283648.html SCOPE_IDENTITY和@@IDENTITY的作 ...
- 03_4_this关键字
03_4_this关键字 1. this关键字 在类的方法定义中使用的this关键字代表使用该方法的对象的引用. 当必须指出当前使用方法的对象是谁时要使用this. 有时使用this可以处理方法中成员 ...
- 使用CAShapeLayer实现复杂的View的遮罩效果
一.案例演示 最近在整理一个聊天的项目的时候,发送图片的时候,会有一个三角的指向效果,指向这张图片的发送者.服务端返回给我们的图片只是一张矩形的图片,我们如何把一张矩形的图片或者View,加上一层自定 ...
- Race condition
在很多门课上都接触到race condition, 其中也举了很多方法解决这个问题.于是想来总结一下这些方法. Race condition 它旨在描述一个系统或者进程的输出依赖于不受控制的事件出现顺 ...
- 拓扑排序 topsort
拓扑排序 对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序 ...
- Thinkphp5的安装
很长没有码代码了,现在开始做这件事情的意义已经完全与以前不一样了.因为最近有相当长的一段休息时间,是个学习的好时间啊.之前接触过TP3.2,听说后来的版本有挺大的改动,因此呢,现在终于有时间可以好好的 ...
- Linux菜鸟起飞之路【十】进程管理
一.基本概念 进程:程序的一次动态执行 守护进程:在后台运行并提供系统服务的进程 僵尸进程:完成了生命周期但却依然留在进程表中的进程,我们称之为 “僵尸进程” 父进程:指已创建一个或多个子进程的进程. ...
- 搭建本地虚拟服务器linux(CentOS 7)的python虚拟环境(Hyper-V演示)
新建虚拟机->安装CentOS7->新建虚拟交换机:内部网络->CentOS7设置->网络适配器:虚拟交换机:新建虚拟交换机->进入CentOS # cd /etc/sy ...
- Ubuntu简单指令和热键的学习
Ubuntu查看本机版本的方法 sudo lsb_release -a即可 注销linux: 输入:exit 注意,离开系统不是关机,基本上,linux本身已经有相当多的工作进行,所以你离开时,这次这 ...
- SpringBoot接收前端参数的三种方法
都是以前的笔记了,有时间就整理出来了,SpringBoot接收前端参数的三种方法,首先第一种代码: @RestController public class ControllerTest { //访问 ...