//na podstawie "Algorytmika Praktyczna" Piotra Stanczyka #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <vector> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second #define MP make_pair #define DBG(X) short wzo[1<<15]; short text[(1<<26) + 10]; short text2[(1<<26) + 10]; int p[1<<15]; int textlen; int textlen2; int wzolen; #define KMPH(z) while(k > 0 && wzo[k] != z[q]) k = p[k]; if(wzo[k] == z[q]) k++; void pattern() { int k = 0, q, m; p[1] = 0; for (q = 1; q < wzolen; q++) { KMPH(wzo); p[q + 1] = k; } } bool KMP() { int k = 0, q, m; m = wzolen; k = 0; for (q = 0; q < textlen; q++) { KMPH(text); if(m == k) { //fun(q - m + 1); return true; k = p[k]; } } return false; } bool KMP2() { int k = 0, q, m; m = wzolen; k = 0; for (q = 0; q < textlen2; q++) { KMPH(text2); if(m == k) { //fun(q - m + 1); return true; k = p[k]; } } return false; } vector<int> kom[512]; int main() { int n, m; scanf("%d%d", &n, &m); wzolen = m; //textlen = m; for (int i = 0; i < n; i++) { int l; scanf("%d", &l); for (int j = 0; j < l; j++) { int b; scanf("%d", &b); kom[i + 1].push_back(b); } } for (int i = 0; i < m; i++) { scanf("%d", wzo + i); } pattern(); //cout << "Wyszukiwanie " << pattern << " w " << text << endl; textlen = 1; if (textlen == n && wzo[0] == 1) { printf("1\n"); return 0; } int s = 0; text[0] = 1; textlen = 1; int r = 2; for (int i = 0; ; i++) { s = 1 - s; if (s == 1) { textlen2 = 0; for (int j = 0; j < textlen; j++) { int k = text[j]; for (int z = 0; z < kom[k].size(); z++) { text2[textlen2] = kom[k][z]; if (textlen2 >= (1 << 26)) break; DBG(cout << "text2[textlen2] " << text2[textlen2] << " ";) textlen2++; } } //cout << endl; int f = KMP2(); //cout << "f " << f << endl; if (f) { printf("%d\n", r); return 0; } } else { textlen = 0; for (int j = 0; j < textlen2; j++) { int k = text2[j]; for (int z = 0; z < kom[k].size(); z++) { text[textlen] = kom[k][z]; DBG(cout << "text[textlen] " << text[textlen] << " ";) textlen++; if (textlen >= (1 << 26)) break; } } int f = KMP(); //cout << "f " << f << endl; if (f) { printf("%d\n", r); return 0; } } r++; DBG(cout << "r " << r << endl;) if (textlen >= (1 << 26)) break; if (textlen2 >= (1 << 26)) break; } printf("-1\n"); //cout << "f " << f << endl; 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 | //na podstawie "Algorytmika Praktyczna" Piotra Stanczyka #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <vector> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second #define MP make_pair #define DBG(X) short wzo[1<<15]; short text[(1<<26) + 10]; short text2[(1<<26) + 10]; int p[1<<15]; int textlen; int textlen2; int wzolen; #define KMPH(z) while(k > 0 && wzo[k] != z[q]) k = p[k]; if(wzo[k] == z[q]) k++; void pattern() { int k = 0, q, m; p[1] = 0; for (q = 1; q < wzolen; q++) { KMPH(wzo); p[q + 1] = k; } } bool KMP() { int k = 0, q, m; m = wzolen; k = 0; for (q = 0; q < textlen; q++) { KMPH(text); if(m == k) { //fun(q - m + 1); return true; k = p[k]; } } return false; } bool KMP2() { int k = 0, q, m; m = wzolen; k = 0; for (q = 0; q < textlen2; q++) { KMPH(text2); if(m == k) { //fun(q - m + 1); return true; k = p[k]; } } return false; } vector<int> kom[512]; int main() { int n, m; scanf("%d%d", &n, &m); wzolen = m; //textlen = m; for (int i = 0; i < n; i++) { int l; scanf("%d", &l); for (int j = 0; j < l; j++) { int b; scanf("%d", &b); kom[i + 1].push_back(b); } } for (int i = 0; i < m; i++) { scanf("%d", wzo + i); } pattern(); //cout << "Wyszukiwanie " << pattern << " w " << text << endl; textlen = 1; if (textlen == n && wzo[0] == 1) { printf("1\n"); return 0; } int s = 0; text[0] = 1; textlen = 1; int r = 2; for (int i = 0; ; i++) { s = 1 - s; if (s == 1) { textlen2 = 0; for (int j = 0; j < textlen; j++) { int k = text[j]; for (int z = 0; z < kom[k].size(); z++) { text2[textlen2] = kom[k][z]; if (textlen2 >= (1 << 26)) break; DBG(cout << "text2[textlen2] " << text2[textlen2] << " ";) textlen2++; } } //cout << endl; int f = KMP2(); //cout << "f " << f << endl; if (f) { printf("%d\n", r); return 0; } } else { textlen = 0; for (int j = 0; j < textlen2; j++) { int k = text2[j]; for (int z = 0; z < kom[k].size(); z++) { text[textlen] = kom[k][z]; DBG(cout << "text[textlen] " << text[textlen] << " ";) textlen++; if (textlen >= (1 << 26)) break; } } int f = KMP(); //cout << "f " << f << endl; if (f) { printf("%d\n", r); return 0; } } r++; DBG(cout << "r " << r << endl;) if (textlen >= (1 << 26)) break; if (textlen2 >= (1 << 26)) break; } printf("-1\n"); //cout << "f " << f << endl; return 0; } |