HDU 2222 Keywords Search(ACオートマチックの入門問題)
12433 ワード
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18800 Accepted Submission(s): 6269
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.
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.
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
lcy
この問題がACオートマチックです.
AC自動機参考:
http://www.cnblogs.com/destinydesigner/archive/2009/10/15/1584191.html
http://www.cppblog.com/mythit/archive/2009/04/21/80633.html
まとめてみると、ACオートマチックは難しくないですね~~~
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18800 Accepted Submission(s): 6269
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.
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.
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
lcy
この問題がACオートマチックです.
AC自動機参考:
http://www.cnblogs.com/destinydesigner/archive/2009/10/15/1584191.html
http://www.cppblog.com/mythit/archive/2009/04/21/80633.html
まとめてみると、ACオートマチックは難しくないですね~~~
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int MAX=26;
typedef struct Trie_Node
{
struct Trie_Node *fail;//
struct Trie_Node *next[MAX];//26
int count;//
}Trie;
Trie *Q[500010];// , bfs
char keyword[55];
char str[1000010];
int head,tail;//
void insert(Trie *root,char *word)
{
Trie *p=root;
int i=0;
while(word[i]!='\0')
{
if(p->next[word[i]-'a']==NULL)
{
Trie *temp=new Trie;
for(int j=0;j<MAX;j++)
temp->next[j]=NULL;
temp->count=0;
temp->fail=NULL;
p->next[word[i]-'a']=temp;
}
p=p->next[word[i]-'a'];
i++;
}
p->count++;
}
void build_ac(Trie *root)
{
root->fail=NULL;
head=tail=0;
Q[head++]=root;
while(head!=tail)
{
Trie *temp=Q[tail++];
Trie *p=NULL;
for(int i=0;i<MAX;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;
}
Q[head++]=temp->next[i];
}
}
}
}
int query(Trie *root)
{
int i=0;
int cnt=0;
int len=strlen(str);
Trie *p=root;
int index;
while(str[i])
{
index=str[i]-'a';
while(p->next[index]==NULL&&p!=root)p=p->fail;
p=p->next[index];
if(p==NULL)p=root;
Trie *temp=p;
while(temp!=root)
{
cnt+=temp->count;
temp->count=0;
temp=temp->fail;
}
i++;
}
return cnt;
}
void del(Trie *root)
{
for(int i=0;i<MAX;i++)
if(root->next[i]!=NULL)
del(root->next[i]);
free(root);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
Trie *root=new Trie;
root->count=0;
root->fail=NULL;
for(int i=0;i<MAX;i++)
root->next[i]=NULL;
for(int i=0;i<n;i++)
{
scanf("%s",&keyword);
insert(root,keyword);
}
build_ac(root);
scanf("%s",&str);
printf("%d
",query(root));
del(root);//
}
return 0;
}