#include <iostream> #include <vector> #include <unordered_set> #include <algorithm> using namespace std; void wypisz(vector<short> &w) { for (int i=0; i<w.size(); ++i) cout<<w[i]; cout<<endl; } struct H { size_t operator()(const vector<short> &w) const { size_t x=w.size(); for (int i=0; i<w.size(); ++i) { x+=w[i]*(i+3); } return x; } }; struct R { bool operator()(const vector<short> &w1, const vector<short> &w2) const { return w1==w2; } }; int main() { ios_base::sync_with_stdio(false); int n, m; cin>>n>>m; vector<short> h[501], s; for (int i=1; i<=n; ++i) { int ile; cin>>ile; for (int j=0; j<ile; ++j) { int a; cin>>a; h[i].push_back(a); } } for (int i=0; i<m; ++i) { int a; cin>>a; s.push_back(a); } if (m==1 && s[0]==1) { cout<<1<<endl; return 0; } vector<short> pocz, u, x; pocz.push_back(1); unordered_set<vector<short>, H, R> wszystkie(131071); vector<vector<short> > nast, noweNast; wszystkie.insert(pocz); nast.push_back(pocz); int rw=m/2+1, rw21=rw*2-1; int kmp[10000]; { s.push_back(0); int i=0, j=kmp[0]=-1; while (i<m) { while (j>-1 && s[i]!=s[j]) j=kmp[j]; ++i; ++j; if (s[i]==s[j]) kmp[i]=kmp[j]; else kmp[i]=j; } } for (int i=2; i<200000; ++i) { noweNast.clear(); for (int j=0; j<nast.size(); ++j) { vector<short> &w=nast[j]; x.clear(); for (int k=0; k<w.size(); ++k) x.insert(x.end(), h[w[k]].begin(), h[w[k]].end()); { x.push_back(0); int ii=0, jj=0, q=x.size()-1; while (jj<q) { while (ii>-1 && s[ii]!=x[jj]) ii=kmp[ii]; ++ii; ++jj; if (ii>=m) { cout<<i<<endl; return 0; } } x.pop_back(); } for (int k=0; k<(int) x.size()-rw21+1; k+=rw) { u.clear(); u.insert(u.end(), x.begin()+k, x.begin()+k+rw21); if (wszystkie.insert(u).second) noweNast.push_back(u); } u.clear(); u.insert(u.end(), x.begin()+max(0, (int) x.size()-rw21), x.end()); if (wszystkie.insert(u).second) noweNast.push_back(u); } if (noweNast.empty()) break; nast.swap(noweNast); //cerr<<"W: "<<i<<' '<<wszystkie.size()<<' '<<nast.size()<<endl; } 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 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 | #include <iostream> #include <vector> #include <unordered_set> #include <algorithm> using namespace std; void wypisz(vector<short> &w) { for (int i=0; i<w.size(); ++i) cout<<w[i]; cout<<endl; } struct H { size_t operator()(const vector<short> &w) const { size_t x=w.size(); for (int i=0; i<w.size(); ++i) { x+=w[i]*(i+3); } return x; } }; struct R { bool operator()(const vector<short> &w1, const vector<short> &w2) const { return w1==w2; } }; int main() { ios_base::sync_with_stdio(false); int n, m; cin>>n>>m; vector<short> h[501], s; for (int i=1; i<=n; ++i) { int ile; cin>>ile; for (int j=0; j<ile; ++j) { int a; cin>>a; h[i].push_back(a); } } for (int i=0; i<m; ++i) { int a; cin>>a; s.push_back(a); } if (m==1 && s[0]==1) { cout<<1<<endl; return 0; } vector<short> pocz, u, x; pocz.push_back(1); unordered_set<vector<short>, H, R> wszystkie(131071); vector<vector<short> > nast, noweNast; wszystkie.insert(pocz); nast.push_back(pocz); int rw=m/2+1, rw21=rw*2-1; int kmp[10000]; { s.push_back(0); int i=0, j=kmp[0]=-1; while (i<m) { while (j>-1 && s[i]!=s[j]) j=kmp[j]; ++i; ++j; if (s[i]==s[j]) kmp[i]=kmp[j]; else kmp[i]=j; } } for (int i=2; i<200000; ++i) { noweNast.clear(); for (int j=0; j<nast.size(); ++j) { vector<short> &w=nast[j]; x.clear(); for (int k=0; k<w.size(); ++k) x.insert(x.end(), h[w[k]].begin(), h[w[k]].end()); { x.push_back(0); int ii=0, jj=0, q=x.size()-1; while (jj<q) { while (ii>-1 && s[ii]!=x[jj]) ii=kmp[ii]; ++ii; ++jj; if (ii>=m) { cout<<i<<endl; return 0; } } x.pop_back(); } for (int k=0; k<(int) x.size()-rw21+1; k+=rw) { u.clear(); u.insert(u.end(), x.begin()+k, x.begin()+k+rw21); if (wszystkie.insert(u).second) noweNast.push_back(u); } u.clear(); u.insert(u.end(), x.begin()+max(0, (int) x.size()-rw21), x.end()); if (wszystkie.insert(u).second) noweNast.push_back(u); } if (noweNast.empty()) break; nast.swap(noweNast); //cerr<<"W: "<<i<<' '<<wszystkie.size()<<' '<<nast.size()<<endl; } cout<<-1<<endl; return 0; } |