AC自动机 hdu2222

 1 #include <iostream>
 2 using namespace std;
 3
 4 struct Node{
 5     Node *next[26];
 6     Node* fail;
 7     int count;
 8     Node(){
 9         for (int i = 0; i < 26; i++){
10             next[i] = NULL;
11         }
12         fail = NULL;
13         count = 0;
14     }
15 };
16 char words[51], s[1000001];
17 Node* d[500001];
18 void insert(char words[], Node *root){
19     int i, len = strlen(words), v;
20     Node *p = root;
21     for (i = 0; i < len; i++){
22         v = words[i] - ‘a‘;
23         if (p->next[v] == NULL){
24             p->next[v] = new Node();
25         }
26         p = p->next[v];
27     }
28     p->count++;
29 }
30 void build(Node *root){
31     int head, tail, i;
32     Node *p, *temp;
33     head = 0;
34     tail = 0;
35     root->fail = NULL;
36     d[head] = root;
37     while (head <= tail){
38         temp = d[head++];
39         for (i = 0; i < 26; i++){
40             if (temp->next[i] == NULL) continue;
41             if (temp == root){
42                 temp->next[i]->fail = root;
43             }
44             else{
45                 p = temp->fail;
46                 while (p != NULL){
47                     if (p->next[i] != NULL){
48                         temp->next[i]->fail = p->next[i];
49                         break;
50                     }
51                     p = p->fail;
52                 }
53                 if (p == NULL){
54                     temp->next[i]->fail = root;
55                 }
56             }
57             d[++tail] = temp->next[i];
58         }
59     }
60 }
61
62 int query(char s[], Node* root){
63     int ans = 0, len = strlen(s), i, v;
64     Node *p = root, *temp;
65     for (i = 0; i < len; i++){
66         v = s[i] - ‘a‘;
67         while (p->next[v] == NULL && p != root){
68             p = p->fail;
69         }
70         p = (p->next[v] != NULL) ? p->next[v] : root;
71         temp = p;
72         while (temp != root&&temp->count != -1){
73             ans += temp->count;
74             temp->count = -1;
75             temp = temp->fail;
76         }
77     }
78     return ans;
79 }
80
81 int main(){
82     int m, n, i;
83     Node *root;
84     cin >> m;
85     while (m--){
86         root = new Node();
87         cin >> n;
88         for (i = 0; i < n; i++){
89             cin >> words;
90             insert(words, root);
91         }
92         build(root);
93         cin >> s;
94         cout << query(s, root) << endl;
95     }
96 }

时间: 2024-10-13 17:00:04

AC自动机 hdu2222的相关文章

AC自动机 HDU2222 Keywords Search

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 62674    Accepted Submission(s): 20749 Problem Description In the modern time, Search engine came into the life of everybody lik

【HDU2222】Keywords Search(AC自动机)

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

hdu2222 AC自动机-给定串中出现了几个模式串

http://acm.hdu.edu.cn/showproblem.php?pid=2222 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 lo

ac自动机基础模板(hdu2222)

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, th

hdu2222 Keywords Search &amp; AC自动机学习小结

传送门:http://http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:AC自动机入门题,直接上AC自动机即可. 对于构建AC自动机,我们要做的只有三件事: 1)构建字典树 2)构建失败指针 3)构建trie图(这道题好像不做这一步也能A...但是这一步不做是会被卡成O(n^2)的...) 1)第一步还是比较好理解的 根是虚根,边代表字母,那么根到终止节点的路径就是一个字符串,这样对于前缀相同的字符串我们就可以省下存公共前缀的空间. 加入一个模式

HDU2222 AC自动机

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 59781    Accepted Submission(s): 19700 Problem Description In the modern time, Search engine came into the life of everybody lik

hdu2222(ac自动机模板)

先推荐两篇写的很好的ac自动机blog: http://blog.csdn.net/creatorx/article/details/71100840 http://blog.csdn.net/niushuai666/article/details/7002823 正题 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意: 给出 n 个模式串以及一个 主串, 问有多少个模式串在主串中出现过 思路: ac自动机模板题 代码: 1 #inc

HDU2222 Keywords Search(AC自动机)

AC自动机是一种多模式匹配的算法.大概过程如下: 首先所有模式串构造一棵Trie树,Trie树上的每个非根结点都代表一个从根出发到该点路径的字符串. 然后每个结点都计算出其fail指针的值,这个fail指针就指向这个结点所表示字符串的最长存在的后缀所对应的结点,如果不存在就指向根:计算每个结点的fail用BFS,比如当前结点u出队要拓展并计算其孩子结点的fail,v是其第k个孩子,fail[v]的值就是某个fail[fail[fail...[u]]]存在第k孩子结点其第k个孩子结点,如果不存在f

HDU2222 Keywords Search【AC自动机】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意: 给你N个模式串,和一个文本串.问:文本串中共出现了几个模式串. 思路: 这道题是AC自动机的基础题目.就是求文本串中出现的模式串个数.用Val[]数组来标记模式串. 最后用ans累加模式串个数. AC代码: #include<iostream> #include<algorithm> #include<cstdio> #include<cstrin