Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33300 Accepted Submission(s): 10762
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
解决方案:AC自动机的模板题
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct trie
{
struct trie *child[26];///字典树的26个指针
struct trie *fail;///字典树的fail指针
int cnt;///标记防止出现相同单词
trie()
{
for(int i=0; i<26; i++)
child[i]=NULL;
fail=NULL;
cnt=0;
}
};
trie *root;
int ans;
char word[55],text[1000004];
void create(char *word)
{
trie *s=root;
int i=0;
while(word[i]!='\0')
{
int c=word[i]-'a';
if(s->child[c]==NULL)
{
s->child[c]=new trie();///新的节点
}
s=s->child[c];
i++;
}
s->cnt++;
}///建立动态字典树
void create_fail()
{
queue<trie *>Q;
Q.push(root);///先是根节点
trie *s;
while(!Q.empty())
{
s=Q.front();///根节点出队列
Q.pop();
for(int i=0; i<26; i++)
{
if(s->child[i]!=NULL)
{
if(s==root) s->child[i]->fail=root;///根节点的儿子的fail都指向根节点
else
{
trie *temp=s;
while(temp->fail!=NULL)
{
if(temp->fail->child[i]!=NULL)
{
s->child[i]->fail=temp->fail->child[i];
break;
}
temp=temp->fail;
}///沿其父节点fail指向的节点往上寻,有i,则该节点的fail指向它的儿子
if(temp->fail==NULL)
{
s->child[i]->fail=root;
}///若寻不到,则指向root
}///若不是根节点,追随其父节点的fail指针找
Q.push(s->child[i]);///寻到的加入队列
}
}
}
}///构造fail指针
void AC_search()
{
int i=0;
trie *p=root;
while(text[i])
{
//cout<<"sd";
int id=text[i]-'a';
// cout<<text[i];
while(p!=root&&p->child[id]==NULL)
{
p=p->fail;//cout<<"sd";
}///查询失败,沿fail指针走
if(p->child[id]!=NULL)
{
p=p->child[id];
p=(p==NULL)?root:p;
trie *temp=p;
while(temp!=root&&temp->cnt!=0)
{ //cout<<"sd";
ans+=temp->cnt;
temp->cnt=0;
temp=temp->fail;
}///记录出现过的单词
}
i++;
//cout<<i<<endl;
}
}
int main()
{
int t;
scanf("%d",&t);
int N;
while(t--)
{
root=new trie;
scanf("%d",&N);
for(int i=0; i<N; i++)
{
scanf("%s",word);
create(word);
}
scanf("%s",text);
ans=0;
create_fail();
AC_search();
printf("%d\n",ans);
}
return 0;
}