前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu----(2222)Keywords Search(ac自动机)

hdu----(2222)Keywords Search(ac自动机)

作者头像
Gxjun
发布2018-03-26 15:06:00
6690
发布2018-03-26 15:06:00
举报
文章被收录于专栏:mlml

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 35930    Accepted Submission(s): 11597

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

ac自动机:  构造一颗Trie树,像kmp一样构造一个失败指针,进行记录;

代码:

代码语言:javascript
复制
  1 #define LOCAL
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<queue>
  5 #include<iostream>
  6 using namespace std;
  7 struct Trie
  8 {
  9   struct Trie *fail;
 10   struct Trie *child[26];
 11   int tail;   //末尾标记
 12 };
 13 
 14 void _insert(char *s,Trie *root)     //构造一个Trie树
 15 {
 16   Trie *newcur,*cur;
 17     cur=root;
 18   for(int i=0;s[i];i++)
 19   {
 20       if(cur->child[s[i]-'a']==NULL)
 21     {
 22         newcur= new Trie ;
 23         for(int j=0;j<26;j++)
 24           newcur->child[j]=NULL;
 25           newcur->fail=NULL;
 26           newcur->tail=0;
 27           cur->child[s[i]-'a']=newcur;
 28     }
 29      cur=cur->child[s[i]-'a'];
 30   }
 31   cur->tail++;  //有可能有重复的单词
 32 }
 33 
 34 //构造失败指针
 35 void ac_fail(Trie * root)
 36 {
 37    queue<Trie*>tree;
 38    Trie *fro,*q;
 39      tree.push(root);
 40    while(!tree.empty())
 41    {
 42        fro=tree.front();
 43        tree.pop();
 44        for(int i=0;i<26;i++){
 45       if(fro->child[i]!=NULL)
 46       {
 47           if(fro==root)
 48             fro->child[i]->fail=root;  //将他的下一个函数的指针的失败指针指向当前指针
 49         else
 50         {
 51             q=fro;
 52             while(q->fail)
 53             {
 54                if(q->fail->child[i]){
 55                  fro->child[i]->fail=q->fail->child[i];
 56                  break;
 57             }
 58             q=q->fail;
 59            }
 60          if(!q->fail)  fro->child[i]->fail=root;
 61        }
 62         tree.push(fro->child[i]);
 63      }
 64     }
 65    }
 66 }
 67 
 68 int query(char *s,Trie *root)
 69 {
 70   Trie *cur=root,*newcur;
 71   int ans=0;
 72   for(int i=0;s[i];i++)
 73   {
 74       while(cur->child[s[i]-'a']==NULL&&cur!=root)
 75         cur=cur->fail;
 76     cur=cur->child[s[i]-'a'];
 77     if(cur==NULL) cur=root;
 78      newcur=cur;
 79     while(newcur!=root&&newcur->tail>0)
 80     {
 81       ans+=newcur->tail;
 82       newcur->tail=0;
 83       newcur=newcur->fail;
 84     }
 85   }
 86   return ans;
 87 }
 88 char s1[51];
 89 char t1[1000050];     //目标主串
 90 int main()
 91 {
 92   #ifdef LOCAL
 93   freopen("test.in","r",stdin);
 94   #endif
 95   int cas,n;
 96   Trie *root;
 97   scanf("%d",&cas);
 98   while(cas--)
 99   {
100    scanf("%d",&n);
101    root= new Trie;
102    for(int i=0;i<26;i++)
103      root->child[i]=NULL;
104      root->fail=NULL;
105      root->tail=0;
106    while(n--)
107    {
108       scanf("%s",s1);
109          _insert(s1,root);
110    }
111      ac_fail(root);
112      scanf("%s",t1);
113      printf("%d\n",query(t1,root));
114   }
115    return 0;
116 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-10-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Keywords Search
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档