18.10.29 多模式串字符串匹配模板题~AC自动机

描述

给若干个模式串,以及若干个句子,判断每个句子里是否包含模式串。 句子和模式串都由小写字母组成

输入第一行是整数n,表示有n个模式串 ( n <= 1000)
接下来n行每行一个模式串。每个模式串长度不超过20
接下来一行是整数m,表示有m个句子 (m <= 1000)
接下来m行,每行一个句子,每个句子长度不超过1000输出对每个句子,如果包含某个模式串,则输出 YES, 否则输出 NO

样例输入

3
abc
def
gh
2
abcd
ak

样例输出

YES
NO

来源

Xu Yewen

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 #include <set>
13
14 using namespace std;
15 const int maxn = 1005;
16 char line[maxn];
17 int m, n,nodecou;
18 struct node {
19     node*next[26];
20     node*prev;
21     bool isdanger;
22     node() {
23         memset(next, 0, sizeof(next));
24         prev = NULL;
25         isdanger = false;
26     }
27 }tree[maxn*20];
28
29 void build() {
30     for (int i = 0; i < 26; i++)
31         tree[0].next[i] = tree + 1;
32     tree[1].prev = tree;
33     queue<node*>q;
34     q.push(tree+1);
35     while (!q.empty()) {
36         node*now = q.front();
37         q.pop();
38         for (int i = 0; i < 26; i++) {
39             node*child = now->next[i];
40             if (child) {
41                 node*prev = now->prev;
42                 while (prev->next[i] == NULL)
43                     prev = prev->prev;
44                 child->prev = prev->next[i];
45                 if (prev->isdanger)
46                     child->isdanger = true;
47                 q.push(child);
48             }
49         }
50     }
51 }
52
53 void search(char*str) {
54     node*first = tree + 1;
55     for (int i = 0; str[i] != ‘\0‘; i++) {
56         while (first->next[str[i]-‘a‘] == NULL)
57             first = first->prev;
58         if (first->next[str[i]-‘a‘]->isdanger) {
59             printf("YES\n");
60             return;
61         }
62         first = first->next[str[i] - ‘a‘];
63     }
64     printf("NO\n");
65 }
66
67 void init() {
68     scanf("%d", &n);
69     nodecou = 1;
70     for (int i = 1; i <= n; i++) {
71         scanf("%s",line);
72         node *first = tree + 1;
73         int l = strlen(line);
74         for (int i = 0; i!=l ; i++) {
75             if (first->next[line[i] - ‘a‘] == NULL) {
76                 nodecou++;
77                 first->next[line[i] - ‘a‘] = tree + nodecou;
78             }
79             first = first->next[line[i] - ‘a‘];
80             if (i == l - 1)
81                 first->isdanger = true;
82         }
83     }
84     build();
85     scanf("%d", &m);
86     while (m--) {
87         scanf("%s", line);
88         search(line);
89     }
90 }
91
92 int main()
93 {
94     init();
95     return 0;
96 }

预热题

原文地址:https://www.cnblogs.com/yalphait/p/9874009.html

时间: 2024-07-29 01:54:08

18.10.29 多模式串字符串匹配模板题~AC自动机的相关文章

多模式串字符串匹配模板题

总时间限制:  1000ms 内存限制:  65536kB 描述 给若干个模式串,以及若干个句子,判断每个句子里是否包含模式串. 句子和模式串都由小写字母组成 输入 第一行是整数n,表示有n个模式串 ( n <= 1000)接下来n行每行一个模式串.每个模式串长度不超过20接下来一行是整数m,表示有m个句子 (m <= 1000)接下来m行,每行一个句子,每个句子长度不超过1000 输出 对每个句子,如果包含某个模式串,则输出 YES, 否则输出 NO 样例输入 3 abc def gh 2

18.10.29 躲不开的病毒(AC自动机+dfs)

描述 有若干种病毒,每种病毒的特征代码都是一个01串. 每个程序也都是一个01串. 问是否存在不被病毒感染(不包含任何病毒的特征代码)的无限长的程序. 输入第一行是整数n,表示有n个病毒接下来n行,每行是一个由 0,1构成的字符串,表示一个病毒特征代码所有的病毒的特征代码总长度不超过30000输出如果存在无限长的没有被病毒感染的程序,输出 "TAK",否则输出"NIE" 样例输入 样例1: 2 10 11 样例2: 2 1 0 样例输出 样例1: TAK 样例2:

UVALive 5103 Computer Virus on Planet Pandora Description 求模式串出现的种数 AC自动机

题目链接:点击打开链接 题意: case数 n个模式串 一个母串. 问:n个模式串出现的种数(一个模式串多次出现只算一次) 对于 "ABC" , 若母串出现了"CBA"这样的反串,也算出现了. 所以: 1 ABC CBA ans = 1 #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <algorith

18.6 负载均衡集群介绍;18.7 LVS介绍;18.9 LVS NAT模式搭建(上);18.10 LVS NAT模式搭建(下)

扩展: lvs 三种模式详解 http://www.it165.net/admin/html/201401/2248.html lvs几种算法 http://www.aminglinux.com/bbs/thread-7407-1-1.html 关于arp_ignore和 arp_announce http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html lvs原理相关的   http://blog.csdn.net/pi9nc/

多模字符串匹配算法之AC自动机—原理与实现

简介: 本文是博主自身对AC自动机的原理的一些理解和看法,主要以举例的方式讲解,同时又配以相应的图片.代码实现部分也予以明确的注释,希望给大家不一样的感受.AC自动机主要用于多模式字符串的匹配,本质上是KMP算法的树形扩展.这篇文章主要介绍AC自动机的工作原理,并在此基础上用Java代码实现一个简易的AC自动机. 欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 1. 应用场景-多模字符串匹配 我们现在考虑这样一个问题,在一个文本串t

18.10.29 POJ 3987 Computer Virus on Planet Pandora(AC自动机+字符串处理)

描述 Aliens on planet Pandora also write computer programs like us. Their programs only consist of capital letters (‘A’ to ‘Z’) which they learned from the Earth. On planet Pandora, hackers make computer virus, so they also have anti-virus software. Of

18.10.29 POJ 3691 DNA repair(AC自动机+dp)

描述 Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a DNA is represented as a string containing characters 'A', 'G' , 'C' and 'T'. The repairing techniques a

hdu2896 病毒侵袭 AC自动机入门题 N(N &lt;= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M &lt;= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,

/** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串, 题目保证每个待匹配串中最多有三个模式串. 思路:ac自动机做法,字符为可见字符,那么直接就是他们的ascii值作为每一个字符的标志.最多128: 由于不超过三个,所以找到3个就可以re

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