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:
NIE

来源

test cases by 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 = 30005;
 16 int n, nodecou;
 17 char virus[maxn];
 18 bool instack[maxn];
 19
 20 struct node {
 21     int next[2];
 22     int prev;
 23     bool isdanger;
 24     node() {
 25         memset(next, 0, sizeof(next));
 26         prev = -1;
 27         isdanger = false;
 28     }
 29 }tree[maxn];
 30
 31 void insert() {
 32     int now = 1, l = strlen(virus);
 33     for (int i = 0; i < l; i++) {
 34         int idx = virus[i] - ‘0‘;
 35         if (tree[now].next[idx] == 0) {
 36             nodecou++;
 37             tree[now].next[idx] = nodecou;
 38         }
 39         now = tree[now].next[idx];
 40         if (i == l - 1)
 41             tree[now].isdanger = true;
 42     }
 43 }
 44
 45 void build() {
 46     tree[0].next[0] = 1, tree[0].next[1] = 1;
 47     tree[1].prev = 0;
 48     queue<int>q;
 49     q.push(1);
 50     while (!q.empty()) {
 51         int now = q.front(); q.pop();
 52         for (int i = 0; i < 2; i++) {
 53             int child = tree[now].next[i];
 54             int prev = tree[now].prev;
 55             if (child) {
 56                 while (tree[prev].next[i] == NULL)
 57                     prev = tree[prev].prev;
 58                 tree[child].prev = tree[prev].next[i];
 59                 if (tree[tree[child].prev].isdanger)
 60                     tree[child].isdanger = true;
 61                 q.push(child);
 62             }
 63             else {
 64                 while (!tree[prev].next[i])
 65                     prev = tree[prev].prev;
 66                 tree[now].next[i] = tree[prev].next[i];
 67                 if (tree[child].isdanger)
 68                     tree[now].next[i] = 0;
 69             }
 70         }
 71     }
 72 }
 73
 74 bool dfs(int rt) {
 75     if (instack[rt])return true;
 76     instack[rt] = true;
 77     bool ans = false;
 78     for (int i = 0; i < 2; i++)
 79         if (tree[rt].next[i] && !tree[tree[rt].next[i]].isdanger)
 80         {
 81             ans = ans || dfs(tree[rt].next[i]);
 82             if (ans)return true;
 83         }
 84     instack[rt] = false;
 85     return false;
 86 }
 87
 88 void init() {
 89     scanf("%d", &n);
 90     nodecou = 1;
 91     while (n--) {
 92         scanf("%s", virus);
 93         insert();
 94     }
 95     build();
 96     if (dfs(1))
 97         printf("TAK\n");
 98     else
 99         printf("NIE\n");
100 }
101
102 int main()
103 {
104     init();
105     return 0;
106 }

看见大家跑的时间都很少就放心地使用了递归

不用指针就舒爽多了

再也不用担心RE了,快乐

PPT上写的是栈中保存的元素,大概可以用栈来做?反正我是懒得写栈做法

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

时间: 2024-08-02 00:05:47

18.10.29 躲不开的病毒(AC自动机+dfs)的相关文章

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

Hnu 10104 病毒 (AC自动机+dfs)

病毒 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Total submit users: 41, Accepted users: 23 Problem 10104 : No special judgement Problem description 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找

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 多模式串字符串匹配模板题~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

BZOJ 2938 Poi2000 病毒 AC自动机+拓扑排序

题目大意:给定n个01串,问是否存在一个无限长的01串,不包含这n个01串中的任何一个 建出Trie图之后判环即可 我这傻逼一开始居然跑了一个DFS去判环23333 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 30300 using namespace std; int n; char s[M]; namespace Aho_C

八周二次课(1月30日) 10.28 rsync工具介绍 10.29/10.30 rsync常用选项 10.31 rsync通过ssh同步

八周二次课(1月30日)10.28 rsync工具介绍10.29/10.30 rsync常用选项10.31 rsync通过ssh同步===================================================================================================================================================================rsync命令:是一个远程数据同步工具,可

[CareerCup] 18.10 Word Transform 单词转换

18.10 Given two words of equal length that are in a dictionary, write a method to transform one word into another word by changing only one letter at a time. The new word you get in each step must be in the dictionary. 这道题让我们将一个单词转换成另一个单词,每次只能改变一个字母,

背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox

原文:背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox [源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) RichTextBlock RichTextBlockOverflow RichEditBox 示例1.RichTextBlock 的示例Controls/TextControl/RichTextBlockDemo.xaml <Page

10.28 rsync工具介绍 - 10.29/10.30 rsync常用选项 - 10.31 rsync通过ssh同步

- 10.28 rsync工具介绍 - 10.29/10.30 rsync常用选项 - 10.31 rsync通过ssh同步 # 10.28 rsync工具介绍 -/A目录 --> /B目录(A目录更新了一个文件,每次更新都需要把A目录拷贝到B目录),如果用cp命令 比较浪费时间,耗费磁盘空间,磁盘压力 读写之类的, -使用rsync -av /etc/passwd /tmp/1.txt -a选项就是包含了好几个选项  ,v 是可视化,可以看到拷贝的过程 ``` [[email protecte