#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int contains(int *A, int *B) { /* cout << "A: "; for(int i=0; A[i] > 0; i++) cout << " " << A[i]; cout << endl; cout << "B: "; for(int i=0; B[i] > 0; i++) cout << " " << B[i]; cout << endl; */ for (int i=0; A[i] > 0; i++) { for (int j=0; B[j] > 0; j++) { int ok = 1, k; for (k=0; ok == 1 && A[i+k] > 0 && B[j+k] > 0; k++) { ok = A[i+k] == B[j+k]; } if (B[j+k] == 0 && ok) { // cout << i << " " << j << endl; return 1; } } } return 0; } int main() { int n, m; cin >> n >> m; int i, **H; H = (int**) malloc(sizeof(int*) * n); for(i=0; i<n; i++) { int l; cin >> l; H[i] = (int*) malloc(sizeof(int) * (l + 1)); H[i][0] = l; for(int j=1; j<=l; j++) { int h; cin >> h; H[i][j] = h; } } int *S; S = (int*) malloc(sizeof(int*) * (m + 1)); for(i=0; i<m; i++) { cin >> S[i]; } S[i] = 0; int *A, *B, *C; A = (int*) malloc(sizeof(int) * 1000000); B = (int*) malloc(sizeof(int) * 1000000); A[0] = 1; A[1] = B[0] = 0; int age = 1, size = 0; while (size*m < 1000000) { //cout << "." << endl; int x = 0; for(i=0; A[i] > 0; i++) { //cout << "," << endl; for(int j=0; j<H[A[i]-1][0]; j++) { //cout << " " << A[i] << " " << H[A[i]-1][0] << " " << H[A[i]-1][j+1] << endl; B[x++] = H[A[i]-1][j+1]; //cout << ",," << endl; } //cout << ",,," << endl; } //cout << ".." << endl; B[x] = 0; size = x; C = A; A = B; B = C; //cout << "..." << endl; age++; if (contains(A, S)) { cout << age << endl; return 0; } B[0] = 0; } cout << -1 << 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 | #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int contains(int *A, int *B) { /* cout << "A: "; for(int i=0; A[i] > 0; i++) cout << " " << A[i]; cout << endl; cout << "B: "; for(int i=0; B[i] > 0; i++) cout << " " << B[i]; cout << endl; */ for (int i=0; A[i] > 0; i++) { for (int j=0; B[j] > 0; j++) { int ok = 1, k; for (k=0; ok == 1 && A[i+k] > 0 && B[j+k] > 0; k++) { ok = A[i+k] == B[j+k]; } if (B[j+k] == 0 && ok) { // cout << i << " " << j << endl; return 1; } } } return 0; } int main() { int n, m; cin >> n >> m; int i, **H; H = (int**) malloc(sizeof(int*) * n); for(i=0; i<n; i++) { int l; cin >> l; H[i] = (int*) malloc(sizeof(int) * (l + 1)); H[i][0] = l; for(int j=1; j<=l; j++) { int h; cin >> h; H[i][j] = h; } } int *S; S = (int*) malloc(sizeof(int*) * (m + 1)); for(i=0; i<m; i++) { cin >> S[i]; } S[i] = 0; int *A, *B, *C; A = (int*) malloc(sizeof(int) * 1000000); B = (int*) malloc(sizeof(int) * 1000000); A[0] = 1; A[1] = B[0] = 0; int age = 1, size = 0; while (size*m < 1000000) { //cout << "." << endl; int x = 0; for(i=0; A[i] > 0; i++) { //cout << "," << endl; for(int j=0; j<H[A[i]-1][0]; j++) { //cout << " " << A[i] << " " << H[A[i]-1][0] << " " << H[A[i]-1][j+1] << endl; B[x++] = H[A[i]-1][j+1]; //cout << ",," << endl; } //cout << ",,," << endl; } //cout << ".." << endl; B[x] = 0; size = x; C = A; A = B; B = C; //cout << "..." << endl; age++; if (contains(A, S)) { cout << age << endl; return 0; } B[0] = 0; } cout << -1 << endl; return 0; } |