//============================================================================ // Name : .cpp // Author : Grzegorz Gajoch // Description : //============================================================================ #include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <ctime> #include <ctype.h> #include <algorithm> #include <string> #include <string.h> #include <cstring> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <list> using namespace std; #define FOR(i,a,b) for(int i = a; i <= b; ++i) #define FORD(i,a,b) for(int i = a; i >= b; --i) #define REP(x, n) for(int x=0; x < (n); ++x) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define SIZE(n) (int)n.size() #define ALL(c) c.begin(),c.end() #define PB(n) push_back(n) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<vector<int> > VVI; typedef vector<bool> VB; #define MP make_pair #define ST first #define ND second const int INF = 1000000001; //#define DEBUG #ifdef DEBUG #define debug printf #else #define debug // #endif int N, S; vector< vector<int> > Expensions; vector<int> ending; list < int > nowCell; void clear() { nowCell.clear(); nowCell.push_front(1); Expensions.clear(); ending.clear(); } void read() { scanf("%d %d", &N, &S); Expensions.resize(N+1); FOR(i, 1, N) { int len; scanf("%d", &len); REP(j, len) { int a; scanf("%d", &a); Expensions[i].push_back(a); } } REP(i, S) { int a; scanf("%d", &a); ending.push_back(a); } } void print() { debug("cell: "); FOREACH(itt, nowCell) { debug("%d ", *itt); } debug("\n"); } vector<int> T; void KMPPrep() { long long SeqLen = ending.size(); T.resize(SeqLen + 1, -1); for(int i = 1; i <= SeqLen; i++) { int pos = T[i - 1]; while(pos != -1 && ending[pos] != ending[i - 1]) pos = T[pos]; T[i] = pos + 1; } } bool checkForEnd() { //long long SigLen = SignalLength(); long long AllLen = nowCell.size(); long long SeqLen = ending.size(); long long matches = 0; if( AllLen < SeqLen ) return 0; int sp = 0; int kp = 0; list<int>::iterator it = nowCell.begin(); while(sp < AllLen) { while(kp != -1 && (kp == SeqLen || ending[kp] != *it)) kp = T[kp]; kp++; sp++; it++; if(kp == SeqLen) return true; //matches++; } return false; // return matches; } int main(int argc, char **argv) { clear(); read(); KMPPrep(); debug("A"); long long counts = 0; FOR(xxxx, 1, 100000) { //100k iterations debug("iter: %d, len: %d\n",xxxx, nowCell.size()); if( nowCell.size() > 2000000 ) { break; } list<int>::iterator it = nowCell.begin(); while( it != nowCell.end() ) { int v = *it; debug("erasing: %d\n",v); //print(); debug("adding: "); FOREACH(it2, Expensions[v]) { debug("%d ",*it2); nowCell.insert(it, *it2); } debug("\n"); //print(); auto rem = it; rem--; nowCell.erase(it); //print(); debug("\n");debug("\n"); it = rem; it++; } counts++; if( checkForEnd() ) { printf("%lld\n",counts+1); return 0; } } printf("-1\n"); 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 | //============================================================================ // Name : .cpp // Author : Grzegorz Gajoch // Description : //============================================================================ #include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <ctime> #include <ctype.h> #include <algorithm> #include <string> #include <string.h> #include <cstring> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <list> using namespace std; #define FOR(i,a,b) for(int i = a; i <= b; ++i) #define FORD(i,a,b) for(int i = a; i >= b; --i) #define REP(x, n) for(int x=0; x < (n); ++x) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define SIZE(n) (int)n.size() #define ALL(c) c.begin(),c.end() #define PB(n) push_back(n) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<vector<int> > VVI; typedef vector<bool> VB; #define MP make_pair #define ST first #define ND second const int INF = 1000000001; //#define DEBUG #ifdef DEBUG #define debug printf #else #define debug // #endif int N, S; vector< vector<int> > Expensions; vector<int> ending; list < int > nowCell; void clear() { nowCell.clear(); nowCell.push_front(1); Expensions.clear(); ending.clear(); } void read() { scanf("%d %d", &N, &S); Expensions.resize(N+1); FOR(i, 1, N) { int len; scanf("%d", &len); REP(j, len) { int a; scanf("%d", &a); Expensions[i].push_back(a); } } REP(i, S) { int a; scanf("%d", &a); ending.push_back(a); } } void print() { debug("cell: "); FOREACH(itt, nowCell) { debug("%d ", *itt); } debug("\n"); } vector<int> T; void KMPPrep() { long long SeqLen = ending.size(); T.resize(SeqLen + 1, -1); for(int i = 1; i <= SeqLen; i++) { int pos = T[i - 1]; while(pos != -1 && ending[pos] != ending[i - 1]) pos = T[pos]; T[i] = pos + 1; } } bool checkForEnd() { //long long SigLen = SignalLength(); long long AllLen = nowCell.size(); long long SeqLen = ending.size(); long long matches = 0; if( AllLen < SeqLen ) return 0; int sp = 0; int kp = 0; list<int>::iterator it = nowCell.begin(); while(sp < AllLen) { while(kp != -1 && (kp == SeqLen || ending[kp] != *it)) kp = T[kp]; kp++; sp++; it++; if(kp == SeqLen) return true; //matches++; } return false; // return matches; } int main(int argc, char **argv) { clear(); read(); KMPPrep(); debug("A"); long long counts = 0; FOR(xxxx, 1, 100000) { //100k iterations debug("iter: %d, len: %d\n",xxxx, nowCell.size()); if( nowCell.size() > 2000000 ) { break; } list<int>::iterator it = nowCell.begin(); while( it != nowCell.end() ) { int v = *it; debug("erasing: %d\n",v); //print(); debug("adding: "); FOREACH(it2, Expensions[v]) { debug("%d ",*it2); nowCell.insert(it, *it2); } debug("\n"); //print(); auto rem = it; rem--; nowCell.erase(it); //print(); debug("\n");debug("\n"); it = rem; it++; } counts++; if( checkForEnd() ) { printf("%lld\n",counts+1); return 0; } } printf("-1\n"); return 0; } |