#include #include #include #include #include #include #include #include using namespace std; #ifdef _MSC_VER typedef unsigned __int64 uint64_t; typedef unsigned __int32 uint32_t; #endif typedef uint32_t ui; struct card { char suit; ui rank; card(string s) { if(s.length() != 2) fprintf(stderr, "Bad card: %s\n", s.c_str()); suit=s[1]; if(s[0] >= '2' && s[0] <= '9') rank = s[0]-'0'; else switch(s[0]) { case 'T': rank = 10; break; case 'J': rank = 11; break; case 'Q': rank = 12; break; case 'K': rank = 13; break; case 'A': rank = 14; break; default: fprintf(stderr, "Bad rank: %c\n", s[0]); rank=0; }; } bool operator < (const card &x) const { if(suit == x.suit) return rank < x.rank; else return suit < x.suit; } bool operator == (const card &x) const { return suit == x.suit && rank == x.rank; } }; bool playerhas(const list &hand, char suit) { for(list::const_iterator it = hand.begin(); it != hand.end(); ++it) { if(it->suit == suit) return true; } return false; } void process(string s) { set total; list hands[4]; ui control; for(ui i = 0; i < 4; i++) { if(i>0) { char buf[1024]; cin.getline(buf, 1024); s = buf; } istringstream ss(s); for(ui j = 0; j < 13; j++) { string cardstr; ss >> cardstr; card c(cardstr); if(c.suit == 'C' && c.rank == 2) control = i; total.insert(c); hands[i].push_back(c); } } if(total.size() != 52) { fprintf(stderr, "duplicates\n"); printf("Invalid\n"); return; } // Play the game #define pc(n) hands[n].front() for(ui i=1; i<=13; i++) { fprintf(stderr, "Player %lu leads round %lu\n", control+1, i); char suit = pc(control).suit; ui highrank = pc(control).rank, leader=control; hands[control].pop_front(); for(ui j=0; j<4; j++) { if(j==control) continue; if(pc(j).suit != suit && playerhas(hands[j], suit)) { fprintf(stderr, "Player %lu didn't follow suit in round %lu\n", j+1, i); printf("Invalid\n"); return; } if(pc(j).suit == suit && pc(j).rank > highrank) { highrank = pc(j).rank; leader = j; } hands[j].pop_front(); } control = leader; } printf("Valid\n"); } int main() { while(cin) { string s; char buf[1024]; cin.getline(buf, 1024); s = string(buf); if(s.empty()) continue; process(s); } return 0; }