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.
题目就是给n个单词和一篇文章,问这篇文章总共包含了以上单词中的多少。
ac自动机模版题。就是trie树加上kmp的思想。
代码:
#include <iostream>
#include <cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 1000002;
char s[1000100];
int n;
struct node
{
int cnt;
node* next[26];
node* fail;
node()
{
cnt=0;
fail=NULL;
memset(next,NULL,sizeof(next));
}
};
node *root;
void bfs()
{
node *p=root,*tmp,*son;
queue<node*> q;
q.push(p);
while(!q.empty())
{
tmp=q.front();
q.pop();
for(int i=0; i<26; i++)
{
son=tmp->next[i]; //队首元素的子节点
if(son!=NULL)
{
p=tmp->fail; //p作为保存节点 表示类似kmp当前k的值
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
son->fail=p->next[i];
break;
}
p=p->fail;
}
if(!p) son->fail=root;
q.push(son);
}
}
}
}
void insert() //和trie树一样的插入方式
{
node *p=root;
int len=strlen(s);
for(int i=0; i<len; i++)
{
int index=s[i]-'a';
//printf("%d\n",index);
if(p->next[index]==NULL)
{
p->next[index]=new node();
}
p=p->next[index];
}
p->cnt++;
}
void query()
{
int ans=0;
node* p=root,*tmp;
int len=strlen(s);
for(int i=0; i<len; i++)
{
int index=s[i]-'a';
while(p!=root&&p->next[index]==NULL) //kmp中的k=next[k]的相同思路
{
p=p->fail;
}
p=p->next[index];//下一节点
if(p==NULL)//匹配失败 j=0
{
p=root;
}
tmp=p;
while(tmp!=root) //与kmp不同,找寻所有满足子条件的节点。
{
if(tmp->cnt>=0)
{
ans+=tmp->cnt;
tmp->cnt=-1;
}
else
break;
tmp=tmp->fail;
}
}
printf("%d\n",ans);
}
int main()
{
int tt=0,T;
scanf("%d",&T);
while(T--)
{
root = new node();
scanf("%d\n",&n);
for(int i=0; i<n; i++)
{
scanf("%s",s);
//printf("%s\n",s);
insert();
}
bfs();
scanf("%s",s);
query();
}
return 0;
}