HDU2222(AC自动机)

Keywords Search

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


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
 
 1 //2016.10.09
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 
 8 const int K = 26;
 9 const int N = 500010;
10 
11 struct node//AC自动机中的节点
12 {
13     node *ch[K], *fail;
14     int match;
15     void Clear(){
16         memset(this, 0, sizeof(node));
17     }
18 }*que[N];
19 
20 struct AC_automaton
21 {
22     node nodes[N], *root, *superRoot, *cur;
23     node* newNode(){
24         cur->Clear();
25         return cur++;
26     }
27     void Clear(){//初始化字典树
28         cur = nodes;
29         superRoot = newNode();
30         root = newNode();
31         root->fail = superRoot;
32         for(int i = 0; i < K; i++)
33               superRoot->ch[i] = root;
34         superRoot->match = -1;
35     }
36     void Insert(char *s){//向字典树中插入一个字符串
37         node *t = root;
38         for(; *s; s++){
39             int p = *s-'a';
40             if(t->ch[p] == NULL)
41                   t->ch[p] = newNode();
42             t = t->ch[p];
43         }
44         t->match++;
45     }
46     void build(){//构建自动机
47         int p = 0, q = 0;
48         que[q++] = root;
49         while(p != q){
50             node *t = que[p++];
51             for(int i = 0; i < K; i++){
52                 if(t->ch[i]){
53                     t->ch[i]->fail = t->fail->ch[i];
54                     que[q++] = t->ch[i];
55                 }else{
56                     t->ch[i] = t->fail->ch[i];
57                 }
58             }
59         }
60     }
61     int run(char* s){//计算str串中模式串出现的次数
62         int ans = 0;
63         node *t = root;
64         for(; *s; s++){
65             int p = *s-'a';
66             t = t->ch[p];
67             for(node *u = t; u->match != -1; u = u->fail){
68                 ans += u->match;
69                 u->match = -1;
70             }
71         }
72         return ans;
73     }
74 };
75 
76 char str[1000005];
77 AC_automaton ac;
78 
79 int main()
80 {
81     int n, T;
82     scanf("%d", &T);
83     while(T--)
84     {
85         scanf("%d", &n);
86         getchar();
87         ac.Clear();
88         for(int i = 0; i < n; i++){
89             scanf("%s", str);
90             ac.Insert(str);
91         }
92         ac.build();
93         scanf("%s", str);
94         printf("%d\n", ac.run(str));
95     }
96 
97     return 0;
98 }

 

 

转载于:https://www.cnblogs.com/Penn000/p/5943622.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值