/* * File: main.cpp * Author: cteam027 * * Created on October 22, 2016, 9:21 AM */ #include #include using namespace std; /* * */ struct Card{ Card( char r, char s ) : rank( r ), suit( s ), next( NULL ) {} char rank; char suit; Card * next; bool match(Card * card){ return card->rank == rank || card->suit == suit; } }; void Join( Card * tmp, Card * tmpNext, Card * c ) { c -> next = tmpNext; tmp -> next = c; } bool Sort( Card *& head, Card *& c ) { if( !head ) { head = c; return true; } Card * tmp = head; while( tmp ) { if( tmp == head ) { if( tmp -> match(c) ) { c -> next = tmp; head = c; return true; } } if( !(tmp -> next) ) { if( tmp -> match( c ) ) { tmp -> next = c; return true; } } if( tmp -> match( c ) && tmp -> next -> match( c )) { Join( tmp, tmp -> next, c ); return true; } tmp = tmp -> next; } return false; } int main(int argc, char** argv) { int n; Card * head = NULL, * tmp = NULL; while( (cin . good()) ) { cin >> n; if( !cin . good() ) break; char rank, suit; bool check = true; for( int i = 0; i < n; i++ ) { cin >> rank >> suit ; //cout << rank << " " << suit << endl; if( !head ) { head = new Card( rank, suit ); continue; } tmp = new Card( rank, suit ); if( !Sort( head, tmp ) ) check = false; } if( check ) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }