Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 68161 Accepted Submission(s): 22993
Problem Description
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, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
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, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Output
Print how many keywords are contained in the description.
Sample Input
1 5 she he say shr her yasherhs
Sample Output
3
Author
Wiskey
题意:给N个串,问你模式串中有几个串。
思路:AC自动机=KMP+字典树,Fail数组跟KMP的Next数组差不多,查询时右指针不断右移,同时计算以当前右指针所在位置结尾的串个数。用指针实现比较快。
# include <iostream>
# include <cstdio>
# include <cstring>
# include <cstdlib>
using namespace std;
struct Node
{
int cnt;
Node *fail;
Node *next[26];
}*queue[500005];
char s[1000005];
char keyword[55];
Node *root;
void Init(Node *root)
{
root->cnt=0;
root->fail=NULL;
for(int i=0;i<26;i++)
root->next[i]=NULL;
}
void Build_trie(char *keyword)
{
Node *p,*q;
int i,v;
int len=strlen(keyword);
for(i=0,p=root;i<len;i++)
{
v=keyword[i]-'a';
if(p->next[v]==NULL)
{
q=(struct Node *)malloc(sizeof(Node));
Init(q);
p->next[v]=q;
}
p=p->next[v];
}
p->cnt++;
}
void Build_AC_automation(Node *root)
{
int head=0,tail=0;
queue[head++]=root;
while(head!=tail)
{
Node *p=NULL;
Node *temp=queue[tail++];
for(int i=0;i<26;i++)
{
if(temp->next[i]!=NULL)
{
if(temp==root)
temp->next[i]->fail=root;
else
{
p=temp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)
temp->next[i]->fail=root;
}
queue[head++]=temp->next[i];
}
}
}
}
int query(Node *root)
{
int i,v,count=0;
Node *p=root;
int len=strlen(s);
for(i=0;i<len;i++)
{
v=s[i]-'a';
while(p->next[v]==NULL && p!=root)
p=p->fail;
p=p->next[v];
if(p==NULL)
p=root;
Node *temp=p;
while(temp!=root)
{
if(temp->cnt>=0)
{
count+=temp->cnt;
temp->cnt=-1;
}
else
break;
temp=temp->fail;
}
}
return count;
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
root=(struct Node *)malloc(sizeof(Node));
Init(root);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",keyword);
Build_trie(keyword);
}
Build_AC_automation(root);
scanf("%s",s);
printf("%d\n",query(root));
}
return 0;
}
本文介绍了一个基于AC自动机的图像检索系统关键词匹配问题。通过构建AC自动机,该系统能够高效地从图像描述中匹配用户输入的多个关键词,并详细展示了AC自动机的构建过程及查询算法。
1164

被折叠的 条评论
为什么被折叠?



