#include #include #include #include #include struct card { char rank; char color; bool used; }; int num_cards; int color_cnt[13]; int rank_cnt[4]; inline int color2idx(char c){ switch(c){ case 'C': return 0; case 'D': return 1; case 'H': return 2; case 'S': return 3; } } inline int& color_count(char c){ return color_cnt[color2idx(c)]; } inline int rank2idx(char c){ switch(c){ case 'A': return 0; case '2': return 1; case '3': return 2; case '4': return 3; case '5': return 4; case '6': return 5; case '7': return 6; case '8': return 7; case '9': return 8; case 'X': return 9; case 'J': return 10; case 'Q': return 11; case 'K': return 12; } } inline int& rank_count(char c){ return rank_cnt[rank2idx(c)]; } bool try_next(card last, const std::vector& cards) { if (num_cards == 0) return true; std::vector send_down(cards); std::sort(send_down.begin(), send_down.end(), [&](const card& l, const card& r){ if (l.used && !r.used){ return false; } if (!l.used && r.used){ return true; } auto l_possible = color_count(l.color) + rank_count(l.rank); auto r_possible = color_count(r.color) + rank_count(r.rank); return l_possible < r_possible; }); for (auto& c : send_down){ if (c.used){ break; } if (last.color == 0 || c.color == last.color || c.rank == last.rank) { color_count(c.color)--; rank_count(c.rank)--; c.used = true; num_cards--; if (try_next(c, send_down)) return true; num_cards++; c.used = false; rank_count(c.rank)++; color_count(c.color)++; } } return false; } int main() { std::ios_base::sync_with_stdio(false); int n; while (std::cin >> n) { std::vector cards; std::memset(rank_cnt, 0, sizeof(rank_cnt)); std::memset(color_cnt, 0, sizeof(color_cnt)); std::string tmp; for (int i=0; i> tmp; auto color = tmp[1]; auto rank = tmp[0]; color_count(color)++; rank_count(rank)++; cards.push_back({rank, color, false}); } num_cards = n; if (n >= 37 || try_next({0, 0, true}, cards)){ std::cout << "YES\n"; } else { std::cout << "NO\n"; } } return 0; }