HDU 2222 AC自动机(模版题)

Keywords Search

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

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

Recommend

lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341

题意:给出一个字典和一个模式串,问模式串中出现几个字典中的单词

代码:

  1 ////#include "bits/stdc++.h"
  2 #include "cstdio"
  3 #include "map"
  4 #include "set"
  5 #include "cmath"
  6 #include "queue"
  7 #include "vector"
  8 #include "string"
  9 #include "cstring"
 10 #include "time.h"
 11 #include "iostream"
 12 #include "stdlib.h"
 13 #include "algorithm"
 14 #define db double
 15 #define ll long long
 16 //#define vec vector<ll>
 17 #define Mt  vector<vec>
 18 #define ci(x) scanf("%d",&x)
 19 #define cd(x) scanf("%lf",&x)
 20 #define cl(x) scanf("%lld",&x)
 21 #define pi(x) printf("%d\n",x)
 22 #define pd(x) printf("%f\n",x)
 23 #define pl(x) printf("%lld\n",x)
 24 #define rep(i, x, y) for(int i=x;i<=y;i++)
 25 const int N   = 1e6 + 5;
 26 const int mod = 1e9 + 7;
 27 const int MOD = mod - 1;
 28 const db  eps = 1e-18;
 29 const db  PI  = acos(-1.0);
 30 using namespace std;
 31 const int M = 26;
 32 queue<int> q;
 33 vector<string> vec;
 34 bool vis[N];
 35 struct Trie {
 36     int trieN;
 37     int ch[N][M], val[N], fail[N];
 38     void init() {
 39         memset(vis,0,sizeof(vis));
 40         trieN = -1;
 41         newnode();
 42     }
 43     int newnode() {
 44         memset(ch[++trieN], 0, sizeof(ch[0]));
 45         val[trieN] = fail[trieN] = 0;
 46         return trieN;
 47     }
 48     void insert(const string &str, int index) {
 49         int cur = 0;
 50         for (int i = 0;str[i];i++) {
 51             int d = str[i] - ‘a‘;
 52             if (!ch[cur][d])
 53                 ch[cur][d] = newnode();
 54             cur = ch[cur][d];
 55         }
 56         if (val[cur]) vis[index] = 1;
 57         else val[cur] = index;
 58     }
 59     void build() {
 60         for (int i = 0;i < M;i++) {
 61             if (ch[0][i])
 62                 q.push(ch[0][i]);
 63         }
 64         while (!q.empty()) {
 65             int cur = q.front(); q.pop();
 66             for (int i = 0;i < M;i++) {
 67                 int &next = ch[cur][i];
 68                 if (next) {
 69                     fail[next] = ch[fail[cur]][i];
 70                     q.push(next);
 71                 }
 72                 else next = ch[fail[cur]][i];
 73             }
 74         }
 75     }
 76     void query(const string &str) {
 77         int cur = 0, tmp;
 78         for (int i = 0;str[i];i++) {
 79             int d = str[i] - ‘a‘;
 80             tmp = cur = ch[cur][d];
 81             while (tmp && ~val[tmp]) {
 82                 if (val[tmp] != -1) vis[val[tmp]] = 1;
 83                 val[tmp] = -1;
 84                 tmp = fail[tmp];
 85             }
 86         }
 87     }
 88 }ac;
 89
 90 int main(){
 91     ios::sync_with_stdio(false);
 92     cin.tie(0);
 93     int t;
 94     cin >> t;
 95     while(t--){
 96         ac.init();   //初始化
 97         int n;
 98         string str;
 99         cin >> n;
100         vec.resize(n+1);
101         for(int i = 1;i <= n;i++){
102             cin >> vec[i];
103             ac.insert(vec[i],i);
104         }
105         ac.build();
106         cin>>str;
107         ac.query(str);   //查询是否在str里出现过
108         int ans=0;
109         for(int i = 1;i <= n;i++){
110             if (vis[i]) ans++;
111         }
112         pi(ans);
113     }
114     return 0;
115 }
时间: 2024-08-27 18:25:07

HDU 2222 AC自动机(模版题)的相关文章

HDU 2222 AC自动机模板题

题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 char key[55]; 6 char des[1111111]; 7 struct node{ 8 node *fail; 9 node *next[26]; 10 int cnt;

hdu 2222 AC自动机(模板题)

<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非常多的时候,耗时会非常多,所以这里用到了AC自动机,这是一种类似于Trie树的数据结构,但是同时,它也用到了KMP算法中 next数组的思想. 下面是AC自动机指针形式的题解: #include <stdio.h> #include <stdlib.h> #include <st

HDU 5384 AC自动机模版

点击打开链接 题意:n个A,m个B,对于每个A,输出所有B在A中出现的次数 思路:和AC自动机模版题2222相同,唯一的区别就是n个A不能开二维,自己处理一下就OK了 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll

HDU 2222-Keywords Search-AC自动机模版题

纯粹的模版... 学习模版总会是一个快乐的过程.... #include <cstdio> #include <cstdlib> #include <string> #include <climits> #include <iostream> #include <vector> #include <set> #include <cmath> #include <cctype> #include &l

HDU 2896 病毒侵袭(AC自动机模版题)

AC自动模版题,中文题目就不叙述题意了啊. AC自动主要是构造出字典树之后找到fail指针的跳转,类似于KMP里面的next数组的跳转啊,注意这里是多模式跳转.意思就是这个串跳到下一个串的什么位置啊. 先帖一下,做多了再一起总结吧. 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11347    Accepted Submi

HDU 3065 (AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模式串,且每种模式串出现了多少次. 解题思路: AC自动机模板题.模式串的范围是大写字母,但是匹配串的范围却是(0~127). 如果Trie 开到 128 加上不回收内存,就会MLE. 实际上开到26就行了,find的时候对于c<0||c>26,强制令pos=root出现失配,并开始下一个字符就行了

HDU 2896 (AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串. 解题思路: AC自动机模板题.注意一下字符范围. cnt记录这个模式串的个数改为这个模式串的index. find的时候,把找到的index压入vector里面即可. 注意有多个匹配串,每次find之后会把last->cnt修改,原因是防止一个模式串出现了多次被压入vector,所以先备份一下,

HDU ACM 2222-&gt;AC自动机模版题(入门题)

题意:预先给你一些单词,然后给你一个字符串,在判断有多少个单词出现在这个字符串中. 分析:使用AC自动机解决.下面是自己写的类模版.可以直接使用.最后记得释放内存. #include<iostream> #include<queue> using namespace std; #define LETTER_COUNT 26 class AC_Automation { private: struct Node { Node* fail; //失败指针 Node* next[LETTE

HDU 2222 ----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