#include <iostream> #include <string> #include <vector> #include <map> #include <set> using namespace std; #define DEBUG false #define log if (DEBUG) cerr typedef long long ll; typedef unsigned long long ull; const ll MAX_TOTAL_LENGTH = 66000000; static int numberOfTypes, adultLength; static vector<vector<int>> replications; static vector<int> adultSequence; static vector<int> T; static void readData() { cin >> numberOfTypes >> adultLength; replications.resize(numberOfTypes + 1); for (int i = 1; i <= numberOfTypes; i++) { int repLength; cin >> repLength; replications[i].resize(repLength); for (auto& rep : replications[i]) { cin >> rep; } } adultSequence.resize(adultLength); for (auto& adult : adultSequence) { cin >> adult; } } // https://en.wikibooks.org/wiki/Algorithm_Implementation/String_searching/Knuth-Morris-Pratt_pattern_matcher#C.2B.2B static void computeT(const vector<int>& pattern, vector<int>& T) { T.resize(pattern.size() + 1, -1); for(size_t i = 1; i <= pattern.size(); i++) { int pos = T[i - 1]; while(pos != -1 && pattern[pos] != pattern[i - 1]) { pos = T[pos]; } T[i] = pos + 1; } } static bool KMP(const vector<int>& cells, const vector<int>& pattern, const vector<int>& T) { size_t sp = 0; int kp = 0; while(sp < cells.size()) { while(kp != -1 && (kp == pattern.size() || pattern[kp] != cells[sp])) { kp = T[kp]; } kp++; sp++; if (kp == pattern.size()) { return true; } } return false; } static ll computeNextLength(const vector<int>& cells) { ll length = 0; for (auto cell : cells) { length += replications[cell].size(); } return length; } static void solve() { vector<int> cells[2]; int src, dest; int age; int result; src = 0; dest = 1; cells[src].push_back(1); age = 2; computeT(adultSequence, T); while (true) { ll nextLength = computeNextLength(cells[src]); if (cells[src].size() + nextLength > MAX_TOTAL_LENGTH) { result = -1; break; } cells[dest].resize(nextLength); size_t j = 0; for (size_t i = 0; i < cells[src].size(); i++) { const vector<int>& replication = replications[cells[src][i]]; for (int rep : replications[cells[src][i]]) { cells[dest][j++] = rep; } } if (KMP(cells[dest], adultSequence, T)) { result = age; break; } src = 1 - src; dest = 1 - dest; age ++; } cout << result; } static int findMinRep() { size_t mini = 1001; for (size_t i = 1; i < replications.size(); i++) { mini = min(mini, replications[i].size()); } return mini; } static void logCouple(const pair<vector<int>, vector<int>>& couple) { if (DEBUG) { log << "["; for (auto a : couple.first) log << a << ","; log << "] -> "; for (auto a : couple.second) log << a; log << endl; } } static void logVector(const vector<int>& vec) { if (DEBUG) { for (auto a : vec) log << a << ","; log << endl; } } static void solveShort() { map<vector<int>, vector<int>> doubles; for (int i = 1; i <= numberOfTypes; i++) { vector<int> key = { i }; doubles[key] = replications[i]; for (int j = 1; j <= numberOfTypes; j++) { vector<int> key = { i, j }; doubles[key] = replications[i]; doubles[key].insert(doubles[key].end(), replications[j].begin(), replications[j].end()); } } if (DEBUG) { log << "Doubles:" << endl; for (auto& couple : doubles) { logCouple(couple); } } set<vector<int>> visited; set<vector<int>> toVisit; vector<int> one = { 1 }; toVisit.insert(adultSequence); visited.insert(adultSequence); int age = 2; int result = -1; while (!toVisit.empty()) { set<vector<int>> newToVisit; for (const vector<int>& seq : toVisit) { vector<int> t; computeT(seq, t); logVector(seq); for (auto& couple : doubles) { if (KMP(couple.second, seq, t)) { log << "found: "; logCouple(couple); if (couple.first == one) { result = age; goto end; } if (visited.find(couple.first) == visited.end()) { visited.insert(couple.first); newToVisit.insert(couple.first); } } } } toVisit = newToVisit; age ++; } end: cout << result; } int main() { readData(); if (adultLength == 1 && adultSequence[0] == 1) { cout << 1; } else { if (findMinRep() * 2 >= adultLength) { log << "short" << endl; solveShort(); } else { log << "full" << endl; solve(); } } 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | #include <iostream> #include <string> #include <vector> #include <map> #include <set> using namespace std; #define DEBUG false #define log if (DEBUG) cerr typedef long long ll; typedef unsigned long long ull; const ll MAX_TOTAL_LENGTH = 66000000; static int numberOfTypes, adultLength; static vector<vector<int>> replications; static vector<int> adultSequence; static vector<int> T; static void readData() { cin >> numberOfTypes >> adultLength; replications.resize(numberOfTypes + 1); for (int i = 1; i <= numberOfTypes; i++) { int repLength; cin >> repLength; replications[i].resize(repLength); for (auto& rep : replications[i]) { cin >> rep; } } adultSequence.resize(adultLength); for (auto& adult : adultSequence) { cin >> adult; } } // https://en.wikibooks.org/wiki/Algorithm_Implementation/String_searching/Knuth-Morris-Pratt_pattern_matcher#C.2B.2B static void computeT(const vector<int>& pattern, vector<int>& T) { T.resize(pattern.size() + 1, -1); for(size_t i = 1; i <= pattern.size(); i++) { int pos = T[i - 1]; while(pos != -1 && pattern[pos] != pattern[i - 1]) { pos = T[pos]; } T[i] = pos + 1; } } static bool KMP(const vector<int>& cells, const vector<int>& pattern, const vector<int>& T) { size_t sp = 0; int kp = 0; while(sp < cells.size()) { while(kp != -1 && (kp == pattern.size() || pattern[kp] != cells[sp])) { kp = T[kp]; } kp++; sp++; if (kp == pattern.size()) { return true; } } return false; } static ll computeNextLength(const vector<int>& cells) { ll length = 0; for (auto cell : cells) { length += replications[cell].size(); } return length; } static void solve() { vector<int> cells[2]; int src, dest; int age; int result; src = 0; dest = 1; cells[src].push_back(1); age = 2; computeT(adultSequence, T); while (true) { ll nextLength = computeNextLength(cells[src]); if (cells[src].size() + nextLength > MAX_TOTAL_LENGTH) { result = -1; break; } cells[dest].resize(nextLength); size_t j = 0; for (size_t i = 0; i < cells[src].size(); i++) { const vector<int>& replication = replications[cells[src][i]]; for (int rep : replications[cells[src][i]]) { cells[dest][j++] = rep; } } if (KMP(cells[dest], adultSequence, T)) { result = age; break; } src = 1 - src; dest = 1 - dest; age ++; } cout << result; } static int findMinRep() { size_t mini = 1001; for (size_t i = 1; i < replications.size(); i++) { mini = min(mini, replications[i].size()); } return mini; } static void logCouple(const pair<vector<int>, vector<int>>& couple) { if (DEBUG) { log << "["; for (auto a : couple.first) log << a << ","; log << "] -> "; for (auto a : couple.second) log << a; log << endl; } } static void logVector(const vector<int>& vec) { if (DEBUG) { for (auto a : vec) log << a << ","; log << endl; } } static void solveShort() { map<vector<int>, vector<int>> doubles; for (int i = 1; i <= numberOfTypes; i++) { vector<int> key = { i }; doubles[key] = replications[i]; for (int j = 1; j <= numberOfTypes; j++) { vector<int> key = { i, j }; doubles[key] = replications[i]; doubles[key].insert(doubles[key].end(), replications[j].begin(), replications[j].end()); } } if (DEBUG) { log << "Doubles:" << endl; for (auto& couple : doubles) { logCouple(couple); } } set<vector<int>> visited; set<vector<int>> toVisit; vector<int> one = { 1 }; toVisit.insert(adultSequence); visited.insert(adultSequence); int age = 2; int result = -1; while (!toVisit.empty()) { set<vector<int>> newToVisit; for (const vector<int>& seq : toVisit) { vector<int> t; computeT(seq, t); logVector(seq); for (auto& couple : doubles) { if (KMP(couple.second, seq, t)) { log << "found: "; logCouple(couple); if (couple.first == one) { result = age; goto end; } if (visited.find(couple.first) == visited.end()) { visited.insert(couple.first); newToVisit.insert(couple.first); } } } } toVisit = newToVisit; age ++; } end: cout << result; } int main() { readData(); if (adultLength == 1 && adultSequence[0] == 1) { cout << 1; } else { if (findMinRep() * 2 >= adultLength) { log << "short" << endl; solveShort(); } else { log << "full" << endl; solve(); } } return 0; } |