HDOJ1277 全文检索(AC自动机)

全文检索

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1403    Accepted Submission(s): 458

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]

Author

Cai Minglun

Source

杭电ACM集训队训练赛(VI)

Recommend

lcy   |   We have carefully selected several similar problems for you:  1275 3068 2243 1274 1271

大一测试监考无聊水一把,题目不能一直老做一类啊,要不脑子都不灵活了,本来可以用字典树做的,而我却写了个AC自动机,写得长还费时间

ac代码

#include<stdio.h>
#include<string.h>
char str[60060],key[60],s[60060],k1[10],k2[10],k3[10];
int vis[10010],ans[10010],k,flag;
int head,tail;
struct node
{
    node *fail;
    node *next[10];
    int cnt;
    node()
    {
        fail=NULL;
        cnt=0;
        for(int i=0;i<10;i++)
            next[i]=NULL;
    }
}*q[602000];
node *root;
void insert(char *s,int id)
{
    int temp,len,i;
    node *p=root;
    len=strlen(s);
    for(i=0;i<len;i++)
    {
        temp=s[i]-'0';
        if(p->next[temp]==NULL)
            p->next[temp]=new node();
        p=p->next[temp];
    }
    p->cnt=id;
}
void build_ac()
{
    root->fail=NULL;
    q[tail++]=root;
    while(head!=tail)
    {
        node *p=q[head++];
        node *temp=NULL;
        for(int i=0;i<10;i++)
        {
            if(p->next[i]!=NULL)
            {
                if(p==root)
                {
                    p->next[i]->fail=root;
                }
                else
                {
                    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=root;
                    }
                }
                q[tail++]=p->next[i];
            }
        }
    }
}
void query()
{
    int len=strlen(str);
    node *p=root,*temp;
    for(int i=0;i<len;i++)
    {
        int x=str[i]-'0';
        while(p->next[x]==NULL&&p!=root)
        {
            p=p->fail;
        }
        p=p->next[x];
        if(p==NULL)
        {
            p=root;
        }
        temp=p;
        while(temp!=root&&temp->cnt!=0)
        {
            if(!vis[temp->cnt])
			{
				vis[temp->cnt]=1;
				ans[k++]=temp->cnt;
				flag=1;
			}
            temp=temp->fail;
        }
    }
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i;
		head=tail=0;
		root=new node();
		k=0;
		flag=0;
		memset(vis,0,sizeof(vis));
		for(i=0;i<n;i++)
		{
			scanf("%s",s);
			strcat(str,s);
		}
		//printf("%s",str);
		for(i=1;i<=m;i++)
		{
			int x;
			scanf("[Key No. %d] ",&x);
			scanf("%s%s%s%s",k1,k2,k3,key);
			insert(key,i);
		}
		build_ac();
		query();
		if(!flag)
		{
			printf("No key can be found !\n");
		}
		else
		{
			printf("Found key:");
			for(i=0;i<k;i++)
			{
				printf(" [Key No. %d]",ans[i]);
			}
			printf("\n");
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 14:18:01

HDOJ1277 全文检索(AC自动机)的相关文章

[hdu1277]全文检索(AC自动机)

解题关键:AC自动机模板题,注意字符匹配时若无法匹配,直接用%s即可. 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=12; 5 const int MAXN=600010; 6 int num,ans[10020],nn; 7 bool vis[600010]; 8 bool flag=false; 9 struct Trie{//数组形式 10 int Next[

杭电ACM1277——全文检索~~AC自动机算法

题目的意思:给你一篇文章,再给你T个字符串,判断这T个字符串有哪些在文章中出现过. 由于文章很大,普通的方法必定超时,所以需要用 AC自动机算法. AC自动机算法是多模匹配算法之一,主要是用于在一篇文章中,找出给定的N个单词在这篇文章中出现的个数. AC自动机算法,我也是刚刚学习,主要是在建立字典树的基础上,增加了失败指针,提高了匹配的效率.而且最难的是失败指针的建立. 它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高. 对于AC自动机算法,可以参考大神的博客:点击打开链接 里面有

暑假集训day9补充(AC自动机)

推荐网站http://blog.csdn.net/niushuai666/article/details/7002823 AC自动机嘛,此AC(aho-corasick)非彼AC(Accepted). 我也不是很会解释 有一题是必须打的hdu2222. #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int mn=

ac自动机基础模板(hdu2222)

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a long description, when users type some keywords to find the image, th

HDU 2825 Wireless Password AC自动机+dp

训练赛第二场的I题,上完体育课回来就把这题过了,今天训练赛rank1了,还把大大队虐了,而且我还过了这道题 (虽然我也就过了这道题...),第一次在比赛中手写AC自动机还带dp的,心情大好. 给一个字符串集合,求包含该集合超过K个字符的,长度为L的字符串的个数. 显然是在AC自动机上跑dp,设dp[u][L][k]表示当前在结点u,还要走L步,当前状态为k的个数.一开始第三维表示的是包含k个字符串,但是题目要求不含重复的,那就只能状压了.转移为dp[u][L][k]+=dp[v][L-1][nk

HDU 2896-病毒侵袭(ac自动机)

题意: 给定多个模式串,每给一个母串,输出包含模式串的编号,最后输出包含模式串的母串的数量. 分析: ac自动机模板 #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <st

hdu2222 Keywords Search &amp; AC自动机学习小结

传送门:http://http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:AC自动机入门题,直接上AC自动机即可. 对于构建AC自动机,我们要做的只有三件事: 1)构建字典树 2)构建失败指针 3)构建trie图(这道题好像不做这一步也能A...但是这一步不做是会被卡成O(n^2)的...) 1)第一步还是比较好理解的 根是虚根,边代表字母,那么根到终止节点的路径就是一个字符串,这样对于前缀相同的字符串我们就可以省下存公共前缀的空间. 加入一个模式

hdoj 2896 病毒侵袭(AC自动机)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 思路分析:题目为模式匹配问题,对于一个给定的字符串,判断能匹配多少个模式:该问题需要静态建树,另外需要对AC自动机的模板加以修改, 对于每个匹配的模式的最后一个单词的fail指针指向root,即可实现一个字符串进行多次模式匹配: 代码如下: #include <queue> #include <cstdio> #include <cstring> #include &

从Trie谈到AC自动机

ZJOI的SAM让我深受打击,WJZ大神怒D陈老师之T3是SAM裸题orz...我还怎么混?暂且写篇`从Trie谈到AC自动机`骗骗经验. Trie Trie是一种好玩的数据结构.它的每个结点存的是字母,因此得名`字母树`. 出一张图让大家感受下. (image powered by SaiBu NaoCu) 上面那是一棵插入了 ape,app,applicant,application,bake,ban,banana 等词的Trie.红色结点表示接受态. 显然,查找时只需顺着链照下来,插入只需