#include <stdio.h> #include <stdlib.h> #include <string.h> enum { JACK=11, QUEEN, KING, ACE }; typedef struct { int rank; char rank_char; char suit; } Card; int compare_int(const void *a, const void *b) { return *(const int*)a - *(const int*)b; } int sequential(int *base, int count) { for (int i = 1; i < count; ++i) if (base[i] != base[i - 1] + 1) return 0; return 1; } void n_of_a_kind(int *ranks, int target_count, int *outs) { int idx = 0, prev = 0, count = 0; for (int i = 0; i < 5; ++i) { if (ranks[i] == prev) { ++count; } else { if (count == target_count) outs[idx++] = prev; prev = ranks[i]; count = 1; } } if (count == target_count) outs[idx] = prev; } #define KEY(a, b, c) return ((a) << 16) | ((b) << 8) | (c) int build_cmp_key(Card *cards, int *ranks) { for (int i = 0; i < 5; ++i) ranks[i] = cards[i].rank; qsort(ranks, 5, sizeof(int), &compare_int); if (ranks[0] == 2 && sequential(ranks, 4) && ranks[4] == ACE) { ranks[4] = 1; qsort(ranks, 5, sizeof(int), &compare_int); } int flush = 1; for (int i = 1; i < 5; ++i) if (cards[i].suit != cards[0].suit) flush = 0; int straight = sequential(ranks, 5); int two_of_kind[2] = {0, 0}, three_of_kind = 0, four_of_kind = 0; n_of_a_kind(ranks, 2, two_of_kind); n_of_a_kind(ranks, 3, &three_of_kind); n_of_a_kind(ranks, 4, &four_of_kind); if (flush && straight) KEY(7, 0, 0); if (four_of_kind) KEY(6, four_of_kind, 0); if (three_of_kind && two_of_kind[0]) KEY(5, three_of_kind, 0); if (flush) KEY(4, 0, 0); if (straight) KEY(3, 0, 0); if (three_of_kind) KEY(2, three_of_kind, 0); if (two_of_kind[0]) KEY(1, two_of_kind[1], two_of_kind[0]); KEY(0, 0, 0); } int compare_hands(Card *a, Card *b) { int ranks_a[5], ranks_b[5]; int key_a = build_cmp_key(a, ranks_a); int key_b = build_cmp_key(b, ranks_b); if (key_a != key_b) return key_a - key_b; for (int i = 4; i >= 0; --i) if (ranks_a[i] != ranks_b[i]) return ranks_a[i] - ranks_b[i]; return 0; } void read_hand(Card *cards) { for (int i = 0; i < 5; ++i) { scanf(" %c%c", &cards[i].rank_char, &cards[i].suit); switch (cards[i].rank_char) { case 'T': cards[i].rank = 10; break; case 'J': cards[i].rank = JACK; break; case 'Q': cards[i].rank = QUEEN; break; case 'K': cards[i].rank = KING; break; case 'A': cards[i].rank = ACE; break; default: cards[i].rank = cards[i].rank_char - '0'; } } } void print_hand(Card *cards) { for (int i = 0; i < 5; ++i) printf("%s%c%c", i ? " " : "", cards[i].rank_char, cards[i].suit); puts(""); } int main() { int count; scanf("%d", &count); while (count-- > 0) { Card h1[5], h2[5]; read_hand(h1); read_hand(h2); int result = compare_hands(h1, h2); if (result > 0) print_hand(h1); else if (result < 0) print_hand(h2); else puts("Tie"); } }