Poker Hands

Time limit1sMemory limit128 MB

Summary
Compare two five-card poker hands and decide which ranks higher, handling all standard categories and tie-breaks.
Level

Medium4 of 10

Topics
Implementation, Sorting, Hash map
Solved
No attempts yet

Problem

A poker deck contains 52 cards. Each card has a suit — one of clubs, diamonds, hearts, or spades (written C, D, H, S in the input) — and a value — one of 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A (representing 2, 3, …, 10, jack, queen, king, ace). For scoring, suits are unordered, while values run from lowest (2) to highest (A).

A poker hand is 5 cards. Hands are ranked by the following categories, from lowest to highest:

  • High Card. A hand that fits no higher category is ranked by its highest card; ties break on the next highest card, and so on.
  • Pair. Two of the five cards share a value. Two hands that both hold a pair are ranked by the pair's value; if equal, by the remaining cards in decreasing order.
  • Two Pairs. The hand holds two distinct pairs. Ranked by the higher pair, then the lower pair, then the remaining card.
  • Three of a Kind. Three cards share a value. Ranked by that value.
  • Straight. Five cards with consecutive values. Ranked by the highest card.
  • Flush. Five cards of the same suit. Ranked by High Card rules.
  • Full House. Three cards of one value plus a pair. Ranked by the value of the three cards.
  • Four of a Kind. Four cards share a value. Ranked by that value.
  • Straight Flush. Five cards of the same suit with consecutive values. Ranked by the highest card.

Given several pairs of hands, decide for each pair which hand — if either — ranks higher.

Note: an ace is only the highest value, so A 2 3 4 5 is not a straight.

Input

Several lines. Each line lists 10 cards separated by spaces: the first 5 cards form the hand of the player named Black, and the next 5 form the hand of the player named White. Input continues until end of file.

Output

For each input line, print exactly one of the following:

Black wins.
White wins.
Tie.

Examples1

  1. Example 1

    Input
    2H 3D 5S 9C KD 2C 3H 4S 8C AH
    2H 4S 4C 2D 4H 2S 8S AS QS 3S
    2H 3D 5S 9C KD 2C 3H 4S 8C KH
    2H 3D 5S 9C KD 2D 3H 5C 9S KH
    
    Expected output
    White wins.
    Black wins.
    Black wins.
    Tie.