Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35631 Accepted Submission(s): 11483
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
Recommend
今天刚刚学了一下AC自动机,看了好久还是有点不太明白,那个fail指针的构造没看懂,照着别人的模板敲了一遍,先留着,以后慢慢领悟。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 10000000
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;
struct node
{
int cnt;//标记该单词是否为最后一个节点
node *fail;//失败指针
node *next[26];//Tire每个节点的子节点,大小根据题意而定
}*q[MAXN];
char keyword[51];//输入的单词,模式串
char str[1000010];//主串
int head,tail;
node *root;
void Insert(char *word,node *root)
{
int index,len;
node *p=root,*newnode;
len=strlen(word);
for (int i=0;i<len;i++)
{
index=word[i]-'a';
if (!p->next[index]) //检查该节点是否存在,否则加入trie树
{
newnode=(struct node*)malloc(sizeof(struct node));//申请空间,添加新的节点
for (int j=0;j<26;j++)
newnode->next[j]=NULL;
newnode->cnt=0;
newnode->fail=NULL;
p->next[index]=newnode;
}
p=p->next[index];
}
p->cnt++;//单词结尾节点自增标记
}
void build_ac_automation(node *root)
{
head=0;
tail=1;
q[head]=root;
node *temp,*p;
while (head<tail) //bfs构造Trie树的失败指针
{
//注意,匹配失败时,由fail指针回溯到正确的位置
temp=q[head];
head++;
for (int i=0;i<26;i++)
{
if (temp->next[i])
{
if (temp==root)//root下的第一层节点的fail指针都指向root
temp->next[i]->fail=root;
else
{
p=temp->fail;
while (p)
{
if (p->next[i])
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if (!p)
temp->next[i]->fail=root;
}
q[tail++]=temp->next[i];
}
}
}
}
int query(node *root)
{
int i,num=0,index,len=strlen(str);
node *p=root;
for (i=0;i<len;i++)
{
index=str[i]-'a';
//由失败指针回溯寻找,判断str[i]是否存在于Trie树中
while(!p->next[index]&&p!=root)
p=p->fail;
p=p->next[index];//找到后p指向该节点
if (!p)
p=root;
node *temp=p;
while (temp!=root)
{
if (temp->cnt>=0)//判断该节点是否被访问过
{
num+=temp->cnt;
temp->cnt=-1;
}
else
break;
temp=temp->fail;
}
}
return num;
}
int main()
{
int t,n,ans;
scanf("%d",&t);
while (t--)
{
root=(struct node *)malloc(sizeof(struct node));
for (int i=0;i<26;i++)
root->next[i]=NULL;
root->fail=NULL;
root->cnt=0;
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%s",keyword);
Insert(keyword,root);
}
build_ac_automation(root);
scanf("%s",str);
ans=query(root);
printf("%d\n",ans);
}
return 0;
}
今天的文章HDU 2222 Keywords Search AC自动机分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7051.html