hdu 2222(AC自动机模版题)

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 59827    Accepted Submission(s): 19715

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
AC自动机板子题
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
struct node{
int count;
node *fail;
node *next[];
node(){
fail=NULL;
count=;
memset(next,NULL,sizeof(next));
}
}*q[N];
char str[+];
char keyword[];
int head,tail;
node *root;
void insert(char *s){
node *p=root;
//cout<<3<<endl;
int len=strlen(s);
for(int i=;i<len;i++){
int v=s[i]-'a';
if(p->next[v]==NULL)p->next[v]=new node();
p=p->next[v];
}
p->count++;
// cout<<2<<endl;
}
void build_AC_automation(node *root){
root->fail=NULL;
head=,tail=;
q[head++]=root;
while(head!=tail){
node *p=NULL;
node *temp=q[tail++];
for(int i=;i<;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(node *root){
int i=0,cnt=0,index,len=strlen(str);
node *p=root;
while(str[i]){
index=str[i]-'a';
while(p->next[index]==NULL && p!=root) p=p->fail;
p=p->next[index];
p=(p==NULL)?root:p;
node *temp=p;
while(temp!=root && temp->count!=-1){
cnt+=temp->count;
temp->count=-1;
temp=temp->fail;
}
}
return cnt;
} */
int query(node *root){
int ans=;
//cout<<2<<endl;
int len=strlen(str);
node *p=root;
for(int i=;i<len;i++){
int v=str[i]-'a';
while(p->next[v]==NULL&&p!=root)p=p->fail;
p=p->next[v];
if(p==NULL)p=root;
node *temp=p;
while(temp!=root){
if(temp->count>=){
ans=ans+temp->count;
temp->count=-; }
else
break;
temp=temp->fail;
}
}
//cout<<2<<endl;
return ans;
}
int main(){
int n,t;
scanf("%d",&t);
while(t--){
root=new node();
scanf("%d",&n);
getchar();
for(int i=;i<n;i++){
gets(keyword);
insert(keyword);
}
build_AC_automation(root);
scanf("%s",str);
cout<<query(root)<<endl;
}
}
 
Sample Output
3
上一篇:Java基础之一组有用的类——为标记定义自己的模式(ScanString)


下一篇:HDU 2222 AC自动机(模版题)