#include <cassert> #include <iostream> #include <string> #include <vector> using namespace std; typedef vector< int > seq_t; typedef vector< vector< int > > hist_t; typedef vector< vector< int > > mut_t; int ROZWIAZANIE; mut_t MUTACJE; struct Prefix { int id_komorki; int tail_len; Prefix(int id_komorki, int tail_len) : id_komorki(id_komorki), tail_len(tail_len) {}; }; ostream& operator<< (ostream&, Prefix const&); template< typename T > ostream& operator<< (ostream&, vector< T > const&); bool match_head( seq_t const& tail, seq_t const& head, int len ) { auto ait = ( tail.end() - len ); auto aet = tail.end(); auto bit = ( head.begin() ); auto diff = mismatch(ait, aet, bit); return diff.first == aet; } vector< Prefix > find_prefixes( seq_t const& seq ) { vector< Prefix > wynik; for( int i = 0; i < MUTACJE.size(); ++i ) { seq_t& mutacja = MUTACJE[i]; int rozm_mut = mutacja.size(); for( int j = 0; j < rozm_mut; ++j ) { //int len = min< int >( j+1, seq.size() ); int len = j+1; if( len <= seq.size() ) { if( match_head( mutacja, seq, len )) { assert( len <= seq.size() ); wynik.push_back( Prefix{i, len} ); } } } } return wynik; } struct Lister; ostream& operator<< (ostream&, Lister const&); struct Lister { seq_t const& seq; int ile_komorek; int pref; int l_pref; vector< Prefix > poczatki; vector< int > numery_komorek; vector< int > match_until; Lister( seq_t& seq, int ile_komorek ): pref(-1), seq(seq), ile_komorek(ile_komorek) { poczatki = find_prefixes(seq); l_pref = poczatki.size(); numery_komorek.push_back( -999 ); match_until.push_back( 0 ); } bool have_match() { assert( match_until.size() > 1 ); assert( numery_komorek.size() > 1 ); int start = (match_until.size() > 1)? match_until[match_until.size() - 2] : 0; int end = min< int >( seq.size(), match_until.back() ); int numer_komorki = numery_komorek.back(); auto ait = seq.begin() + start; auto aet = seq.begin() + end; auto bit = MUTACJE[numer_komorki].begin(); auto diff = mismatch( ait, aet, bit ); return diff.first == aet; } bool is_seq_complete() { if( match_until.size() == 0 ) return false; assert( match_until.back() <= seq.size() ); return match_until.back() == seq.size(); } void turn_last_cell() { numery_komorek.back() += 1; match_until.pop_back(); int nowy_rozmiar = match_until.back() + MUTACJE[numery_komorek.back()].size(); match_until.push_back( min< int >(nowy_rozmiar, seq.size() ) ); assert( match_until.back() > 1 ); } void add_dummy_cell() { numery_komorek.push_back( -1 ); match_until.push_back( match_until.back() ); } bool turn_last_cell_until_match_found() { if( numery_komorek.size() == 1 ) { pref += 1; if( pref < l_pref ) { match_until[0] = poczatki[pref].tail_len; return true; } else { match_until[0] = 0; return false; } } else { int indeks_ostatniej_komorki = ile_komorek - 1; while( numery_komorek.back() < indeks_ostatniej_komorki ) { turn_last_cell(); if( have_match() ) { return true; } } return false; } } void make_sequence_from_cells(seq_t& prev_seq) { prev_seq = { poczatki[pref].id_komorki }; copy( numery_komorek.begin() + 1, numery_komorek.end(), back_inserter( prev_seq ) ); } void trace_back() { assert( pref <= l_pref ); if( pref == l_pref ) { assert( numery_komorek.size() == 1 ); assert( match_until.size() == 1 ); } else { assert( numery_komorek.size() > 1 ); assert( match_until.size() > 1 ); numery_komorek.pop_back(); match_until.pop_back(); } } bool next( seq_t& prev_seq ) { while( pref < l_pref ) { if( turn_last_cell_until_match_found() ) { if( is_seq_complete() ) { make_sequence_from_cells( prev_seq ); return true; } else { add_dummy_cell(); } } else { trace_back(); } } return false; } }; void badaj( seq_t seq, int poziom, hist_t historia = {} ) { if( poziom >= ROZWIAZANIE) { return; } if( (seq.size() == 1) && (seq[0] == 0) ) { assert( poziom < ROZWIAZANIE ); ROZWIAZANIE = poziom; return; } Lister l( seq, MUTACJE.size() ); seq_t prev_seq; while( l.next( prev_seq ) ) { badaj( prev_seq, poziom + 1 ); } } int main() { ios_base::sync_with_stdio( false ); int l_komorek; cin >> l_komorek; // 1 .. 500 int dl_sekw; cin >> dl_sekw; // 1 .. 10000 int all_mut = 0; MUTACJE.resize(l_komorek); for( int i = 0; i < l_komorek; ++i ) { int dl_mut; cin >> dl_mut; all_mut += dl_mut; MUTACJE[i].resize( dl_mut ); for( int j = 0; j < dl_mut; ++j ) { int m; cin >> m; MUTACJE[i][j] = ( m - 1 ); } } seq_t seq; seq.resize( dl_sekw ); for( int i = 0; i < dl_sekw; ++i ) { int m; cin >> m; seq[i] = ( m - 1 ); } int inf = dl_sekw + l_komorek + all_mut; ROZWIAZANIE = inf; badaj( seq, 0 ); cout << (( ROZWIAZANIE == inf )? -1 : 1+ROZWIAZANIE ) << "\n"; } ostream& operator<< (ostream& os, Prefix const& p) { os << "kom=" << p.id_komorki << " " << p.tail_len << "(" << MUTACJE[p.id_komorki] << ")"; return os; } template< typename T > ostream& operator<< (ostream& os, vector< T >const& v ) { if( v.size() > 0 ) { os << v[0]; for( int i = 1; i < v.size(); ++i ) { os << " " << v[i]; } } return os; } ostream& operator<< (ostream& os, Lister const& l) { os << "--- Lister\n"; os << " seq = " << l.seq << "\n"; os << " Wybrany prefix " << l.pref << "(max=" << l.l_pref << ") " << l.poczatki[l.pref] << "\n"; os << " Dopsowane komorki " << l.numery_komorek << "\n"; os << " Pozycje match until " << l.match_until << "\n._.\n"; return os; }
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | #include <cassert> #include <iostream> #include <string> #include <vector> using namespace std; typedef vector< int > seq_t; typedef vector< vector< int > > hist_t; typedef vector< vector< int > > mut_t; int ROZWIAZANIE; mut_t MUTACJE; struct Prefix { int id_komorki; int tail_len; Prefix(int id_komorki, int tail_len) : id_komorki(id_komorki), tail_len(tail_len) {}; }; ostream& operator<< (ostream&, Prefix const&); template< typename T > ostream& operator<< (ostream&, vector< T > const&); bool match_head( seq_t const& tail, seq_t const& head, int len ) { auto ait = ( tail.end() - len ); auto aet = tail.end(); auto bit = ( head.begin() ); auto diff = mismatch(ait, aet, bit); return diff.first == aet; } vector< Prefix > find_prefixes( seq_t const& seq ) { vector< Prefix > wynik; for( int i = 0; i < MUTACJE.size(); ++i ) { seq_t& mutacja = MUTACJE[i]; int rozm_mut = mutacja.size(); for( int j = 0; j < rozm_mut; ++j ) { //int len = min< int >( j+1, seq.size() ); int len = j+1; if( len <= seq.size() ) { if( match_head( mutacja, seq, len )) { assert( len <= seq.size() ); wynik.push_back( Prefix{i, len} ); } } } } return wynik; } struct Lister; ostream& operator<< (ostream&, Lister const&); struct Lister { seq_t const& seq; int ile_komorek; int pref; int l_pref; vector< Prefix > poczatki; vector< int > numery_komorek; vector< int > match_until; Lister( seq_t& seq, int ile_komorek ): pref(-1), seq(seq), ile_komorek(ile_komorek) { poczatki = find_prefixes(seq); l_pref = poczatki.size(); numery_komorek.push_back( -999 ); match_until.push_back( 0 ); } bool have_match() { assert( match_until.size() > 1 ); assert( numery_komorek.size() > 1 ); int start = (match_until.size() > 1)? match_until[match_until.size() - 2] : 0; int end = min< int >( seq.size(), match_until.back() ); int numer_komorki = numery_komorek.back(); auto ait = seq.begin() + start; auto aet = seq.begin() + end; auto bit = MUTACJE[numer_komorki].begin(); auto diff = mismatch( ait, aet, bit ); return diff.first == aet; } bool is_seq_complete() { if( match_until.size() == 0 ) return false; assert( match_until.back() <= seq.size() ); return match_until.back() == seq.size(); } void turn_last_cell() { numery_komorek.back() += 1; match_until.pop_back(); int nowy_rozmiar = match_until.back() + MUTACJE[numery_komorek.back()].size(); match_until.push_back( min< int >(nowy_rozmiar, seq.size() ) ); assert( match_until.back() > 1 ); } void add_dummy_cell() { numery_komorek.push_back( -1 ); match_until.push_back( match_until.back() ); } bool turn_last_cell_until_match_found() { if( numery_komorek.size() == 1 ) { pref += 1; if( pref < l_pref ) { match_until[0] = poczatki[pref].tail_len; return true; } else { match_until[0] = 0; return false; } } else { int indeks_ostatniej_komorki = ile_komorek - 1; while( numery_komorek.back() < indeks_ostatniej_komorki ) { turn_last_cell(); if( have_match() ) { return true; } } return false; } } void make_sequence_from_cells(seq_t& prev_seq) { prev_seq = { poczatki[pref].id_komorki }; copy( numery_komorek.begin() + 1, numery_komorek.end(), back_inserter( prev_seq ) ); } void trace_back() { assert( pref <= l_pref ); if( pref == l_pref ) { assert( numery_komorek.size() == 1 ); assert( match_until.size() == 1 ); } else { assert( numery_komorek.size() > 1 ); assert( match_until.size() > 1 ); numery_komorek.pop_back(); match_until.pop_back(); } } bool next( seq_t& prev_seq ) { while( pref < l_pref ) { if( turn_last_cell_until_match_found() ) { if( is_seq_complete() ) { make_sequence_from_cells( prev_seq ); return true; } else { add_dummy_cell(); } } else { trace_back(); } } return false; } }; void badaj( seq_t seq, int poziom, hist_t historia = {} ) { if( poziom >= ROZWIAZANIE) { return; } if( (seq.size() == 1) && (seq[0] == 0) ) { assert( poziom < ROZWIAZANIE ); ROZWIAZANIE = poziom; return; } Lister l( seq, MUTACJE.size() ); seq_t prev_seq; while( l.next( prev_seq ) ) { badaj( prev_seq, poziom + 1 ); } } int main() { ios_base::sync_with_stdio( false ); int l_komorek; cin >> l_komorek; // 1 .. 500 int dl_sekw; cin >> dl_sekw; // 1 .. 10000 int all_mut = 0; MUTACJE.resize(l_komorek); for( int i = 0; i < l_komorek; ++i ) { int dl_mut; cin >> dl_mut; all_mut += dl_mut; MUTACJE[i].resize( dl_mut ); for( int j = 0; j < dl_mut; ++j ) { int m; cin >> m; MUTACJE[i][j] = ( m - 1 ); } } seq_t seq; seq.resize( dl_sekw ); for( int i = 0; i < dl_sekw; ++i ) { int m; cin >> m; seq[i] = ( m - 1 ); } int inf = dl_sekw + l_komorek + all_mut; ROZWIAZANIE = inf; badaj( seq, 0 ); cout << (( ROZWIAZANIE == inf )? -1 : 1+ROZWIAZANIE ) << "\n"; } ostream& operator<< (ostream& os, Prefix const& p) { os << "kom=" << p.id_komorki << " " << p.tail_len << "(" << MUTACJE[p.id_komorki] << ")"; return os; } template< typename T > ostream& operator<< (ostream& os, vector< T >const& v ) { if( v.size() > 0 ) { os << v[0]; for( int i = 1; i < v.size(); ++i ) { os << " " << v[i]; } } return os; } ostream& operator<< (ostream& os, Lister const& l) { os << "--- Lister\n"; os << " seq = " << l.seq << "\n"; os << " Wybrany prefix " << l.pref << "(max=" << l.l_pref << ") " << l.poczatki[l.pref] << "\n"; os << " Dopsowane komorki " << l.numery_komorek << "\n"; os << " Pozycje match until " << l.match_until << "\n._.\n"; return os; } |