Description
There are several kinds of viruses. The characteristic code of each virus is a 01 string.
Each program is also a 01 string.
Ask if there is an infinitely long program that is not infected with the virus (does not contain any virus's feature code.
The first line of the input is an integer N, indicating that there are n viruses.
Next n rows, each row is a string consisting of 0 and 1, representing a virus feature code
The total length of the feature code for all viruses cannot exceed 30000. If there is an infinitely long program that is not infected with the virus, output "tak". Otherwise, output "Nie"
Sample Input
Example 1: 21011 Example 2: 210
Sample output
Example 1: Tak Example 2: Nie
Source
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 }
View code
We were relieved to use recursion when we saw that everyone was running for a short time.
It's much easier without pointers.
No longer need to worry about re, happy
What is written on the PPT is the elements stored in the stack. Can I use the stack to do it? I am too lazy to write stacks.
18.10.29 invisible viruses (AC automatic machines + DFS)