全文检索

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1134    Accepted Submission(s): 357

Problem Description
我们大家经常用google检索信息,但是检索信息的程序是很困难编写的;现在请你编写一个简单的全文检索程序。
问题的描述是这样的:给定一个信息流文件,信息完全有数字组成,数字个数不超过60000个,但也不少于60个;再给定一个关键字集合,其中关键字个数不超过10000个,每个关键字的信息数字不超过60个,但也不少于5个;两个不同的关键字的前4个数字是不相同的;由于流文件太长,已经把它分成多行;请你编写一个程序检索出有那些关键字在文件中出现过。
 
Input
第一行是两个整数M,N;M表示数字信息的行数,N表示关键字的个数;接着是M行信息数字,然后是一个空行;再接着是N行关键字;每个关键字的形式是:[Key No. 1] 84336606737854833158。
 
Output
输出只有一行,如果检索到有关键字出现,则依次输出,但不能重复,中间有空格,形式如:Found key: [Key No. 9] [Key No. 5];如果没找到,则输出形如:No key can be found !。
 
Sample Input
20 10
646371829920732613433350295911348731863560763634906583816269
637943246892596447991938395877747771811648872332524287543417
420073458038799863383943942530626367011418831418830378814827
679789991249141417051280978492595526784382732523080941390128
848936060512743730770176538411912533308591624872304820548423
057714962038959390276719431970894771269272915078424294911604
285668850536322870175463184619212279227080486085232196545993
274120348544992476883699966392847818898765000210113407285843
826588950728649155284642040381621412034311030525211673826615
398392584951483398200573382259746978916038978673319211750951
759887080899375947416778162964542298155439321112519055818097
642777682095251801728347934613082147096788006630252328830397
651057159088107635467760822355648170303701893489665828841446
069075452303785944262412169703756833446978261465128188378490
310770144518810438159567647733036073099159346768788307780542
503526691711872185060586699672220882332373316019934540754940
773329948050821544112511169610221737386427076709247489217919
035158663949436676762790541915664544880091332011868983231199
331629190771638894322709719381139120258155869538381417179544
000361739177065479939154438487026200359760114591903421347697
 
[Key No. 1] 934134543994403697353070375063
[Key No. 2] 261985859328131064098820791211
[Key No. 3] 306654944587896551585198958148
[Key No. 4] 338705582224622197932744664740
[Key No. 5] 619212279227080486085232196545
[Key No. 6] 333721611669515948347341113196
[Key No. 7] 558413268297940936497001402385
[Key No. 8] 212078302886403292548019629313
[Key No. 9] 877747771811648872332524287543
[Key No. 10] 488616113330539801137218227609
 
Sample Output
Found key: [Key No. 9] [Key No. 5]
 
 
 #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自动机的更多相关文章

  1. hdu 1277 AC自动机入门(指针版和数组版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...

  2. hdu 2896 AC自动机

    // hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...

  3. hdu 3065 AC自动机

    // hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...

  4. hdu 5880 AC自动机

    Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. hdu 3065 AC自动机(各子串出现的次数)

    病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. HDU 5384 AC自动机

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:给n个母串,给m个匹配串,求每个母串依次和匹配串匹配,能得到的数目和. 分析:之前并不知道AC ...

  9. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

随机推荐

  1. vue axios 请求 https 的特殊处理

    最近遇到自签发的CA证书,在前端axios请求https请求时,无法自动加载证书. 解决方法:将无法加载的请求在浏览器新窗口手动加载,选择继续连接. 重新加载,问题解决. 根本原因:因为自签发证书,浏 ...

  2. bootstrap 警告(Alerts)

    本章将讲解警告(Alerts)以及bootstrap所提供的用于警告的class类.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添 ...

  3. jqury点击返回顶部代码

    效果请看右下角:代码如下: <div class="totop"><img src="https://www.cnblogs.com/images/cn ...

  4. cocos2dx 使用XMLHttpRequest时回调status为0的问题

    今天使用cocos连接http访问时,使用XMLHttpRequest在pc上反问时正常的返回了status=0,但是在android上去返回status是0,看了一下底层代码, 发现status只有 ...

  5. 配置淘宝镜像,不使用怪异的cnpm

    npm config set registry https://registry.npm.taobao.org --global npm config set disturl https://npm. ...

  6. 【二分 最大流】bzoj1532: [POI2005]Kos-Dicing

    晚上果然不适合调题目 Description Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一个俱乐部. 俱乐部的人时常在一起玩这个游戏然后评选出 ...

  7. python--字符编码理解

    一.字符编码简史: 美国:1963年 ASCII (包含127个字符  占1个字节) 中国:1980年 GB2312 (收录7445个汉字,包括6763个汉字和682个其它符号) 1993年 GB13 ...

  8. Oracle 数据库常用SQL语句(1)

    一.数据定义语句 CREATE:创建表或其它对象 create database test; //创建test数据库 ),sex )); //创建表 ALTER:修改表或其它对象的结构 )); //为 ...

  9. setup/hold 分析

    分析说明:D2:目的寄存器:D1:源寄存器: edge2:下一个时钟上升沿:edge1:当前时钟上升沿:edge0:当前时钟上升沿的前一个时钟沿:如下图: 建立时间:触发器D2(数据要到达目的的地方) ...

  10. 我的Python分析成长之路6

    模块:本质就是.py结尾的文件.从逻辑上组织python代码. 包: 本质就是一个目录,带有__init__.py文件,从逻辑上组织模块. 模块的分类: 1.标准库(内置的模块) 2.开源库(第三方库 ...