HDU 2222 AC自动机

来源:互联网 发布:淘宝宝贝视频制作 编辑:程序博客网 时间:2024/05/21 08:42

Keywords Search

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


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
15shehesayshrheryasherhs
 

Sample Output
3
 
 
学习别人代码
 

#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct trie{ int num; int next[26],fail; trie():fail(0),num(0){memset(next,0,sizeof(next));}} tree[250007];int root,tot;bool visited[250007];

char s[1000010];

void build(int root){ int p=root; for (int i=0;s[i]!='\0';i++) {   if (tree[p].next[s[i]-'a']==0)   tree[p].next[s[i]-'a']=++tot;  p=tree[p].next[s[i]-'a']; } tree[p].num++;}

int que[500007];void build_ac_automatlon(int root){ int p=root; tree[p].fail=0;  int f=0,r=1; que[1]=p;  while (f<r) {  p=que[++f];  for (int i=0;i<26;i++)   if (tree[p].next[i]!=0)   {    que[++r]=tree[p].next[i];        if (p==root)     tree[tree[p].next[i]].fail=root;    else    {     int temp=tree[p].fail;      while (temp)     {      if (tree[temp].next[i]!=0)      {       tree[tree[p].next[i]].fail=tree[temp].next[i];       break;       }      temp=tree[temp].fail;     }     if (temp==0)      tree[tree[p].next[i]].fail=root;    }   } }}   int acauto(int root){ int ans=0; int p=root; for (int i=0;s[i]!='\0';i++) {  ans+=tree[p].num;  tree[p].num=0;    if (tree[p].next[s[i]-'a']!=0)   p=tree[p].next[s[i]-'a'];  else  {   if (p==root)    continue;   p=tree[p].fail;   while (p!=0)   {     ans+=tree[p].num;    tree[p].num=0;    if (tree[p].next[s[i]-'a'])    {     p=tree[p].next[s[i]-'a'];     break;    }    p=tree[p].fail;   }   if (p==0)    p=root;   }    if (visited[p])   continue;   visited[p]=true;   int q=p;  while (q!=0)  {   ans+=tree[q].num;   tree[q].num=0;   q=tree[q].fail;  }  } return ans; }

int main(){ int T; scanf("%d",&T); while (T-->0) {  memset(tree,0,sizeof(tree));  memset(visited,0,sizeof(visited));  root=1;   tot=1;    int n;  scanf("%d",&n);  getchar();  for (int i=1;i<=n;i++)  {   scanf("%s",s);   build(root);  }      build_ac_automatlon(root);       scanf("%s",s);  cout<<acauto(root)<<endl; } return 0;}

0 0
原创粉丝点击