#include <iostream> #include <vector> #include <algorithm> using namespace std; struct list_node { int val; list_node* next; list_node(int _val) : val(_val), next(nullptr) {} }; vector<vector<int>> ciagi; vector<int> pattern; vector<int> lps; unsigned long long kSize = 0; void computeLPSArray() { int len = 0; int i; lps[0] = 0; i = 1; while (i < pattern.size()) { if (pattern[i] == pattern[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } } bool mature(list_node* txt) { int j = 0; while (txt != nullptr) { if (pattern[j] == txt->val) { ++j; txt = txt->next; } if (j == pattern.size()) { return true; } // mismatch after j matches else if (txt != nullptr && pattern[j] != txt->val) { if (j != 0) j = lps[j - 1]; else txt = txt->next; } } return false; } inline list_node* insertAfter(list_node* node, int val) { list_node* n = new list_node(val); n->next = node->next; node->next = n; ++kSize; return n; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, l, l_tmp; cin >> n >> m; ciagi.reserve(n); ciagi.resize(n); for (int i = 0; i < n; ++i) { cin >> l; ciagi[i].reserve(l); ciagi[i].resize(l); for (int j = 0; j < l; ++j) { cin >> ciagi[i][j]; } } pattern.resize(m); lps.resize(m); for (int i = 0; i < m; ++i) { cin >> pattern[i]; } computeLPSArray(); list_node* head = new list_node(1); kSize = 1; unsigned long long prevSize = 0; unsigned long long minuta = 1; int tmp; while (!mature(head)) { if (kSize > 10000000 || (kSize - prevSize) > 5000000) // hue hue, powinno być ok { cout << "NIE"; return 0; } prevSize = kSize; for (auto it = head; it != nullptr; it = it->next) { tmp = it->val - 1; it->val = ciagi[tmp][0]; for (auto i = ciagi[tmp].begin() + 1; i != ciagi[tmp].end(); i++) { it = insertAfter(it, *i); } } minuta++; } cout << minuta; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <iostream> #include <vector> #include <algorithm> using namespace std; struct list_node { int val; list_node* next; list_node(int _val) : val(_val), next(nullptr) {} }; vector<vector<int>> ciagi; vector<int> pattern; vector<int> lps; unsigned long long kSize = 0; void computeLPSArray() { int len = 0; int i; lps[0] = 0; i = 1; while (i < pattern.size()) { if (pattern[i] == pattern[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } } bool mature(list_node* txt) { int j = 0; while (txt != nullptr) { if (pattern[j] == txt->val) { ++j; txt = txt->next; } if (j == pattern.size()) { return true; } // mismatch after j matches else if (txt != nullptr && pattern[j] != txt->val) { if (j != 0) j = lps[j - 1]; else txt = txt->next; } } return false; } inline list_node* insertAfter(list_node* node, int val) { list_node* n = new list_node(val); n->next = node->next; node->next = n; ++kSize; return n; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, l, l_tmp; cin >> n >> m; ciagi.reserve(n); ciagi.resize(n); for (int i = 0; i < n; ++i) { cin >> l; ciagi[i].reserve(l); ciagi[i].resize(l); for (int j = 0; j < l; ++j) { cin >> ciagi[i][j]; } } pattern.resize(m); lps.resize(m); for (int i = 0; i < m; ++i) { cin >> pattern[i]; } computeLPSArray(); list_node* head = new list_node(1); kSize = 1; unsigned long long prevSize = 0; unsigned long long minuta = 1; int tmp; while (!mature(head)) { if (kSize > 10000000 || (kSize - prevSize) > 5000000) // hue hue, powinno być ok { cout << "NIE"; return 0; } prevSize = kSize; for (auto it = head; it != nullptr; it = it->next) { tmp = it->val - 1; it->val = ciagi[tmp][0]; for (auto i = ciagi[tmp].begin() + 1; i != ciagi[tmp].end(); i++) { it = insertAfter(it, *i); } } minuta++; } cout << minuta; return 0; } |