#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 1009, TEN = 11; const int LEFT = 0, RIGHT = 1; const int HH = 2; int hashQ[HH] = {1000003351, 1000008041}; int hashP[HH] = {2801, 3002231}; int hashR[HH] = {531597640, 455940268}; int n, m, x, k; int farLeft[NMAX][TEN], farRight[NMAX][TEN], computed[NMAX], f[NMAX]; int res[NMAX][NMAX], vis[NMAX][NMAX]; vi hash1[HH], hash2[HH], hashSeq[HH]; vi prod[NMAX]; vi adult, suf1, suf2; void add_(int &x, int y, int &j) { x += y; x = x >= hashQ[j] ? x - hashQ[j] : x; } void mult_(int &x, int y, int &j) { x = ((ll) x * y) % hashQ[j]; } vi unFold(vi & seq, int dir) { vi ans; if(dir == LEFT) { FORE(i, 0, seq.size() - 1) { int sym = seq[i]; ans.insert(ans.end(), ALL(prod[sym])); if(ans.size() > m) { ans.pb(n); return ans; } } } else { // RIGHT FORD(i, seq.size() - 1, 0) { int sym = seq[i]; vi tmp = prod[sym]; reverse(ALL(tmp)); ans.insert(ans.end(), ALL(tmp)); if(ans.size() > m) { ans.pb(n); reverse(ALL(ans)); return ans; } } } if(dir == RIGHT) reverse(ALL(ans)); return ans; } vi computeKMP(vi &s) { prd("kmp\n"); vi ans(s.size()); int j = ans[0] = 0; for (int i = 1; i < s.size(); i++) { while (j > 0 && s[j] != s[i]) j = ans[j-1]; if (s[j] == s[i]) j++; ans[i] = j; prd("%d: %d\n", i, ans[i]); } return ans; } vi computeHash(vi &s, int ha) { int hash = 0, x = 1; vi ans(s.size()); REP(i, s.size()) { int a = s[i] + 1; mult_(a, x, ha); mult_(x, hashP[ha], ha); add_(hash, a, ha); ans[i] = hash; //prd("%d: %d\n", i, ans[i]); } return ans; } vi computeReverseHash(vi &hash, int ha) { int x = 1; vi ans(hash.size()); REP(i, hash.size()) { int a = hash.back(); add_(a, hashQ[ha] - (i ? hash[i-1] : 0), ha); mult_(a, x, ha); mult_(x, hashR[ha], ha); ans[i] = a; } return ans; } int hashesOk(vi h1[], vi h2[], int pos1, int pos2) { REP(ha, HH) { prd("hashes %d: %d, %d: %d\n", pos1, h1[ha][pos1], pos2, h2[ha][pos2]); if(h1[ha][pos1] != h2[ha][pos2]) return 0; } return 1; } /*void extendedGcd(int a, int b) { s = 0; old_s = 1; t = 1; old_t = 0; r = b; old_r := a; while (r!=0) { int quotient = old_r / r; //(old_r, r) := (r, old_r - quotient * r) tmp = r; r = old_r - quotient * r; old_r = r; //(old_s, s) := (s, old_s - quotient * s) tmp = s; s = old_s - quotient * s; old_s = tmp; //(old_t, t) := (t, old_t - quotient * t) tmp = t; t = old_t - quotient * t; old_t = tmp; } //output "Bézout coefficients:", (old_s, old_t) //output "greatest common divisor:", old_r //output "quotients by the gcd:", (t, s) }*/ int superWord(int xa, int xb) { // TODO check indeksy if(xa + xb < m) return 0; int oa = xa ? (xa - suf2[m - xa]) : 0, ob = xb ? (xb - suf1[xb - 1]) : 0; int len = xa + xb - m; // czy istnieja ya * oa + yb * ob = len ? if(oa == 0 && ob == 0) return len == 0; if(oa == 0) return (len % ob == 0); if(ob == 0) return (len % oa == 0); /*extendedGcd(oa, ob); int gcd = old_r, ia = old_s, ib = old_t; if(len % gcd != 0) return 0;*/ while((len % ob != 0) && len >= 0) len -= oa; return len >= 0; } void dfs(int s1, int s2) { vis[s1][s2] = 1; int t1 = prod[s1].back(); int t2 = prod[s2][0]; if(!vis[t1][t2] && res[t1][t2] == NMAX * NMAX) dfs(t1, t2); res[s1][s2] = 1 + res[t1][t2]; } int getFarLeft(vi & seqq) { int ans = 0; REP(ha, HH) hashSeq[ha] = computeHash(seqq, ha); for(int i = 0; i < m && i < seqq.size(); i++) if(hashesOk(hash2, hashSeq, m - i - 1, i)) ans = i + 1; return ans; } int getFarRight(vi & seqq) { int ans = 0; REP(ha, HH) { vi tmp = computeHash(seqq, ha); hashSeq[ha] = computeReverseHash(tmp, ha); } for(int i = 0; i < m && i < seqq.size(); i++) if(hashesOk(hash1, hashSeq, i, seqq.size() - 1 - i)) ans = i + 1; return ans; } void compute(int sym) { prd("compute %d\n", sym); computed[sym] = 1; if(m == 1 && adult[0] == sym) { f[sym] = 1; return; } int pp = prod[sym].size(); REP(pi, pp) { int s0 = prod[sym][pi]; if(!computed[s0]) compute(s0); f[sym] = min(f[sym], f[s0] + 1); } prd("status %d\n", f[sym]); REP(pi, pp-1) { int s1 = prod[sym][pi], s2 = prod[sym][pi+1]; f[sym] = min(f[sym], res[s1][s2] + 2); } prd("status %d\n", f[sym]); REP(pi, pp-1) { int s1 = prod[sym][pi], s2 = prod[sym][pi+1]; vi::itr secStart = prod[sym].begin() + pi + 1; vi prod1(prod[sym].begin(), secStart); vi prod2(secStart, prod[sym].end()); REP(di, TEN) { int farRight0 = getFarRight(prod1); int farLeft0 = getFarLeft(prod2); if(superWord(farRight0, farLeft0)) { prd("found unfolded di %d pi %d\n", di, pi); f[sym] = min(f[sym], di + 2); } prod1 = unFold(prod1, RIGHT); prod2 = unFold(prod2, LEFT); } } prd("f[%d] = %d\n", sym, f[sym]); } int main() { scanf("%d%d\n", &n, &m); REP(i, n) { scanf("%d", &k); REP(j, k) { scanf("%d", &x); prod[i].pb(x-1); } f[i] = NMAX * NMAX; } prod[n].pb(n); REP(i, m) { scanf("%d", &x); adult.pb(x-1); } // ------- 1 vi adultRev(adult); reverse(ALL(adultRev)); suf1 = computeKMP(adult); suf2 = computeKMP(adultRev); reverse(ALL(suf2)); REP(ha, HH) { hash1[ha] = computeHash(adult, ha); hash2[ha] = computeReverseHash(hash1[ha], ha); } // ------- 2 REP(sym, n) { prd("symbol %d\n", sym); vi seq; seq.pb(sym); REP(di, TEN) { farLeft[sym][di] = getFarLeft(seq); seq = unFold(seq, LEFT); prd("unfolded left %d:\n", di); dbg FORIT(it, seq, vi) printf("%d ", *it); prd("farLeft %d \n", farLeft[sym][di]); } seq.clear(); seq.pb(sym); REP(di, TEN) { farRight[sym][di] = getFarRight(seq); seq = unFold(seq, RIGHT); prd("unfolded right %d:\n", di); dbg FORIT(it, seq, vi) printf("%d ", *it); prd("farRight %d \n", farRight[sym][di]); } } // ------- 3 REP(sym1, n) REP(sym2, n) { res[sym1][sym2] = NMAX * NMAX; FORD(di, TEN-1, 0) if(superWord(farRight[sym1][di], farLeft[sym2][di])) res[sym1][sym2] = di; } dbg REP(i, m+1) REP(j, m+1) if(superWord(i, j)) printf("superWord %d %d\n", i, j); // ------- 4 REP(sym1, n) REP(sym2, n) { if(!vis[sym1][sym2] && res[sym1][sym2] == NMAX * NMAX) dfs(sym1, sym2); prd("res %d %d = %d\n", sym1, sym2, res[sym1][sym2]); } compute(0); printf("%d\n", f[0] >= NMAX * NMAX ? -1 : f[0]); 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 1009, TEN = 11; const int LEFT = 0, RIGHT = 1; const int HH = 2; int hashQ[HH] = {1000003351, 1000008041}; int hashP[HH] = {2801, 3002231}; int hashR[HH] = {531597640, 455940268}; int n, m, x, k; int farLeft[NMAX][TEN], farRight[NMAX][TEN], computed[NMAX], f[NMAX]; int res[NMAX][NMAX], vis[NMAX][NMAX]; vi hash1[HH], hash2[HH], hashSeq[HH]; vi prod[NMAX]; vi adult, suf1, suf2; void add_(int &x, int y, int &j) { x += y; x = x >= hashQ[j] ? x - hashQ[j] : x; } void mult_(int &x, int y, int &j) { x = ((ll) x * y) % hashQ[j]; } vi unFold(vi & seq, int dir) { vi ans; if(dir == LEFT) { FORE(i, 0, seq.size() - 1) { int sym = seq[i]; ans.insert(ans.end(), ALL(prod[sym])); if(ans.size() > m) { ans.pb(n); return ans; } } } else { // RIGHT FORD(i, seq.size() - 1, 0) { int sym = seq[i]; vi tmp = prod[sym]; reverse(ALL(tmp)); ans.insert(ans.end(), ALL(tmp)); if(ans.size() > m) { ans.pb(n); reverse(ALL(ans)); return ans; } } } if(dir == RIGHT) reverse(ALL(ans)); return ans; } vi computeKMP(vi &s) { prd("kmp\n"); vi ans(s.size()); int j = ans[0] = 0; for (int i = 1; i < s.size(); i++) { while (j > 0 && s[j] != s[i]) j = ans[j-1]; if (s[j] == s[i]) j++; ans[i] = j; prd("%d: %d\n", i, ans[i]); } return ans; } vi computeHash(vi &s, int ha) { int hash = 0, x = 1; vi ans(s.size()); REP(i, s.size()) { int a = s[i] + 1; mult_(a, x, ha); mult_(x, hashP[ha], ha); add_(hash, a, ha); ans[i] = hash; //prd("%d: %d\n", i, ans[i]); } return ans; } vi computeReverseHash(vi &hash, int ha) { int x = 1; vi ans(hash.size()); REP(i, hash.size()) { int a = hash.back(); add_(a, hashQ[ha] - (i ? hash[i-1] : 0), ha); mult_(a, x, ha); mult_(x, hashR[ha], ha); ans[i] = a; } return ans; } int hashesOk(vi h1[], vi h2[], int pos1, int pos2) { REP(ha, HH) { prd("hashes %d: %d, %d: %d\n", pos1, h1[ha][pos1], pos2, h2[ha][pos2]); if(h1[ha][pos1] != h2[ha][pos2]) return 0; } return 1; } /*void extendedGcd(int a, int b) { s = 0; old_s = 1; t = 1; old_t = 0; r = b; old_r := a; while (r!=0) { int quotient = old_r / r; //(old_r, r) := (r, old_r - quotient * r) tmp = r; r = old_r - quotient * r; old_r = r; //(old_s, s) := (s, old_s - quotient * s) tmp = s; s = old_s - quotient * s; old_s = tmp; //(old_t, t) := (t, old_t - quotient * t) tmp = t; t = old_t - quotient * t; old_t = tmp; } //output "Bézout coefficients:", (old_s, old_t) //output "greatest common divisor:", old_r //output "quotients by the gcd:", (t, s) }*/ int superWord(int xa, int xb) { // TODO check indeksy if(xa + xb < m) return 0; int oa = xa ? (xa - suf2[m - xa]) : 0, ob = xb ? (xb - suf1[xb - 1]) : 0; int len = xa + xb - m; // czy istnieja ya * oa + yb * ob = len ? if(oa == 0 && ob == 0) return len == 0; if(oa == 0) return (len % ob == 0); if(ob == 0) return (len % oa == 0); /*extendedGcd(oa, ob); int gcd = old_r, ia = old_s, ib = old_t; if(len % gcd != 0) return 0;*/ while((len % ob != 0) && len >= 0) len -= oa; return len >= 0; } void dfs(int s1, int s2) { vis[s1][s2] = 1; int t1 = prod[s1].back(); int t2 = prod[s2][0]; if(!vis[t1][t2] && res[t1][t2] == NMAX * NMAX) dfs(t1, t2); res[s1][s2] = 1 + res[t1][t2]; } int getFarLeft(vi & seqq) { int ans = 0; REP(ha, HH) hashSeq[ha] = computeHash(seqq, ha); for(int i = 0; i < m && i < seqq.size(); i++) if(hashesOk(hash2, hashSeq, m - i - 1, i)) ans = i + 1; return ans; } int getFarRight(vi & seqq) { int ans = 0; REP(ha, HH) { vi tmp = computeHash(seqq, ha); hashSeq[ha] = computeReverseHash(tmp, ha); } for(int i = 0; i < m && i < seqq.size(); i++) if(hashesOk(hash1, hashSeq, i, seqq.size() - 1 - i)) ans = i + 1; return ans; } void compute(int sym) { prd("compute %d\n", sym); computed[sym] = 1; if(m == 1 && adult[0] == sym) { f[sym] = 1; return; } int pp = prod[sym].size(); REP(pi, pp) { int s0 = prod[sym][pi]; if(!computed[s0]) compute(s0); f[sym] = min(f[sym], f[s0] + 1); } prd("status %d\n", f[sym]); REP(pi, pp-1) { int s1 = prod[sym][pi], s2 = prod[sym][pi+1]; f[sym] = min(f[sym], res[s1][s2] + 2); } prd("status %d\n", f[sym]); REP(pi, pp-1) { int s1 = prod[sym][pi], s2 = prod[sym][pi+1]; vi::itr secStart = prod[sym].begin() + pi + 1; vi prod1(prod[sym].begin(), secStart); vi prod2(secStart, prod[sym].end()); REP(di, TEN) { int farRight0 = getFarRight(prod1); int farLeft0 = getFarLeft(prod2); if(superWord(farRight0, farLeft0)) { prd("found unfolded di %d pi %d\n", di, pi); f[sym] = min(f[sym], di + 2); } prod1 = unFold(prod1, RIGHT); prod2 = unFold(prod2, LEFT); } } prd("f[%d] = %d\n", sym, f[sym]); } int main() { scanf("%d%d\n", &n, &m); REP(i, n) { scanf("%d", &k); REP(j, k) { scanf("%d", &x); prod[i].pb(x-1); } f[i] = NMAX * NMAX; } prod[n].pb(n); REP(i, m) { scanf("%d", &x); adult.pb(x-1); } // ------- 1 vi adultRev(adult); reverse(ALL(adultRev)); suf1 = computeKMP(adult); suf2 = computeKMP(adultRev); reverse(ALL(suf2)); REP(ha, HH) { hash1[ha] = computeHash(adult, ha); hash2[ha] = computeReverseHash(hash1[ha], ha); } // ------- 2 REP(sym, n) { prd("symbol %d\n", sym); vi seq; seq.pb(sym); REP(di, TEN) { farLeft[sym][di] = getFarLeft(seq); seq = unFold(seq, LEFT); prd("unfolded left %d:\n", di); dbg FORIT(it, seq, vi) printf("%d ", *it); prd("farLeft %d \n", farLeft[sym][di]); } seq.clear(); seq.pb(sym); REP(di, TEN) { farRight[sym][di] = getFarRight(seq); seq = unFold(seq, RIGHT); prd("unfolded right %d:\n", di); dbg FORIT(it, seq, vi) printf("%d ", *it); prd("farRight %d \n", farRight[sym][di]); } } // ------- 3 REP(sym1, n) REP(sym2, n) { res[sym1][sym2] = NMAX * NMAX; FORD(di, TEN-1, 0) if(superWord(farRight[sym1][di], farLeft[sym2][di])) res[sym1][sym2] = di; } dbg REP(i, m+1) REP(j, m+1) if(superWord(i, j)) printf("superWord %d %d\n", i, j); // ------- 4 REP(sym1, n) REP(sym2, n) { if(!vis[sym1][sym2] && res[sym1][sym2] == NMAX * NMAX) dfs(sym1, sym2); prd("res %d %d = %d\n", sym1, sym2, res[sym1][sym2]); } compute(0); printf("%d\n", f[0] >= NMAX * NMAX ? -1 : f[0]); return 0; } |