#include <stdio.h> #define L 10000000 int n,m; int *gro[500], lens[500], vals[1000], valp, pat[100], clen = 1; short comp1[L],*comp=comp1,comp2[L]; int bad; int EXPAND() { short *compb = comp == comp1 ? comp2 : comp1; int i, j, r = 0; for (i = 0; i < clen; i++) { for (j = 0; j < lens[comp[i]]; j++) { compb[r++] = gro[comp[i]][j]; // if (r < 50) printf("%d ", 1+compb[r-1]); if (r > L) { bad = 1; return 0; } } } // puts(""); clen = r; comp = compb; return 1; } int FIND(short *a, int alen, short *b, int blen) { int i,j; for (i = 0; i < alen - blen; i++) { for (j = 0; j < blen; j++) { if (a[i+j] != b[j]) break; } if (j == blen) return 1; } return 0; } int main() { int i,j; scanf("%d%d",&n,&m); for (i = 0; i < n; i++) { scanf("%d", lens+i); gro[i] = vals + valp; for (j = 0; j < lens[i]; j++) { scanf("%d", vals + valp); vals[valp++]--; } } for (i = 0; i < m; i++) { scanf("%d", pat + i); pat[i]--; } i = 1; do { if(FIND(comp,clen,pat,m)) break; i++; } while (EXPAND()); printf ("%d\n", bad ? -1 : i); 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 | #include <stdio.h> #define L 10000000 int n,m; int *gro[500], lens[500], vals[1000], valp, pat[100], clen = 1; short comp1[L],*comp=comp1,comp2[L]; int bad; int EXPAND() { short *compb = comp == comp1 ? comp2 : comp1; int i, j, r = 0; for (i = 0; i < clen; i++) { for (j = 0; j < lens[comp[i]]; j++) { compb[r++] = gro[comp[i]][j]; // if (r < 50) printf("%d ", 1+compb[r-1]); if (r > L) { bad = 1; return 0; } } } // puts(""); clen = r; comp = compb; return 1; } int FIND(short *a, int alen, short *b, int blen) { int i,j; for (i = 0; i < alen - blen; i++) { for (j = 0; j < blen; j++) { if (a[i+j] != b[j]) break; } if (j == blen) return 1; } return 0; } int main() { int i,j; scanf("%d%d",&n,&m); for (i = 0; i < n; i++) { scanf("%d", lens+i); gro[i] = vals + valp; for (j = 0; j < lens[i]; j++) { scanf("%d", vals + valp); vals[valp++]--; } } for (i = 0; i < m; i++) { scanf("%d", pat + i); pat[i]--; } i = 1; do { if(FIND(comp,clen,pat,m)) break; i++; } while (EXPAND()); printf ("%d\n", bad ? -1 : i); return 0; } |