您的位置:首页 > 其它

AC自动机(1)--hdu2222(基本模板)

2016-08-28 23:22 274 查看
                                                                                                             


                                                   Keywords Search

                                                                 Time Limit: 2000/1000 MS (Java/Others)    Memory
Limit: 65536/32768 K (Java/Others)


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


Hint

        这道题题意:给出n个串,然后给一篇文章,问这n个串有多少个在文章里面出现过。就是一个典型的AC自动机,

不了解的可以看我上一篇博客,值得注意的是n个串可能有相同的,需按照不同串处理。所以重复的串需要记下出现的次数,模板代码如下:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<malloc.h>
#include<queue>
using namespace std;
char str[1000000];
struct node{
int cnt; //记录以该字符结尾的相同字符串有几个
node *next[30];
node *fail;
void init()
{
int i;
for(i=0;i<26;i++)
next[i]=NULL;
cnt=0;
fail=NULL;
}
};
node *root;
void insert(){ //建字典树
node *p=root;
for(int i=0;i<strlen(str);i++){
int pos=str[i]-'a';
if(p->next[pos]==NULL){
p->next[pos]=new node;
p->next[pos]->init();
p=p->next[pos];
}
else p=p->next[pos];
}
p->cnt++;
}
void GetFail(){ //得到每个节点的失败指针
int i;
node *temp;
queue<node *>q;
q.push(root);
while(!q.empty()){
temp=q.front();
q.pop();
for(i=0;i<26;i++){
if(!temp->next[i])continue;
q.push(temp->next[i]);
if(temp==root)temp->next[i]->fail=root;
node *p=temp->fail;
while(p!=NULL){
if(p->next[i]!=NULL){
temp->next[i]->fail=p->next[i];
break;
}
else p=p->fail;
if(!p) temp->next[i]->fail=root;
}
}
}
}
int Query(){ //匹配
int i;
int ans=0;
int len=strlen(str);
node *p=root;
for(i=0;i<len;i++){
int index=str[i]-'a';
while(!p->next[index]&&p!=root){
p=p->fail;
}
if(p->next[index]==NULL)continue;
p=p->next[index];
node *temp=p;
while(temp->cnt!=0&&temp!=root){
ans+=temp->cnt;
temp->cnt=0;
temp=temp->fail;
}
}
return ans;
}
int main()
{
int T,n,i;
scanf("%d",&T);
while(T--){
root=new node;
root->init();
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s",str);
insert();
}
GetFail();
scanf("%s",str);
printf("%d\n",Query());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: