#include <bits/stdc++.h>
using namespace std;
const int N=1e3+5;
vector<int> h[N];
int s[N];
vector<int> b1, b2;
int n, m;
bool ok()
{
for(int i=0; i+m<=b1.size(); ++i)
{
bool good=1;
for(int j=1; j<=m; ++j) if(s[j]!=b1[i+j-1]) good=0;
if(good) return 1;
}
return 0;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i=1; i<=n; ++i)
{
int l, tmp;
scanf("%d", &l);
for(int j=0; j<l; ++j)
{
scanf("%d", &tmp);
h[i].push_back(tmp);
}
}
for(int i=1; i<=m; ++i) scanf("%d", &s[i]);
b1.push_back(1);
int cnt;
for(cnt=1; !ok(); ++cnt)
{
for(int j: b1) for(int k: h[j]) b2.push_back(k);
b1.clear();
swap(b1, b2);
}
printf("%d\n", cnt);
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 | #include <bits/stdc++.h> using namespace std; const int N=1e3+5; vector<int> h[N]; int s[N]; vector<int> b1, b2; int n, m; bool ok() { for(int i=0; i+m<=b1.size(); ++i) { bool good=1; for(int j=1; j<=m; ++j) if(s[j]!=b1[i+j-1]) good=0; if(good) return 1; } return 0; } int main() { scanf("%d%d", &n, &m); for(int i=1; i<=n; ++i) { int l, tmp; scanf("%d", &l); for(int j=0; j<l; ++j) { scanf("%d", &tmp); h[i].push_back(tmp); } } for(int i=1; i<=m; ++i) scanf("%d", &s[i]); b1.push_back(1); int cnt; for(cnt=1; !ok(); ++cnt) { for(int j: b1) for(int k: h[j]) b2.push_back(k); b1.clear(); swap(b1, b2); } printf("%d\n", cnt); return 0; } |
English