This page is still under construction.

Parts of this page are still being built. What you see may change.

Rhinoceros Beetle

Interview

Time limit2sMemory limit128 MB

Summary
Given shared community cards and each player's two hole cards, evaluate every player's best five-card poker hand and print the indices of all winners.
Level

Medium5 of 10

Topics
Implementation, Sorting, Combinatorics, Simulation
Solved
No attempts yet

Problem

Rhinoceros beetles are famously strong and, because of that, have been pitted against each other in gambling fights in several countries. After animal-protection groups protested against these events, many gamblers switched to card games such as Texas Hold'em Poker instead. With many players at the table it can be hard to tell who holds the strongest hand. A large tournament, the Arthropoda Card Marathon, is coming up soon, so write a program that decides which hand is the strongest.

Input

The input consists of several instances and is read until end of file.

For each instance:

  • The first line contains one integer NN (1≤N≤101 \le N \le 10), the number of players.
  • The second line contains exactly five community cards, separated by spaces, shared by every player.
  • Each of the next NN lines contains the two cards held by one player, separated by a space: the first of these lines belongs to Player 1, the second to Player 2, and so on.

Each card is written with exactly two characters. The first character is the rank, one of 2 3 4 5 6 7 8 9 X J Q K A, where X stands for the 10. The second character is the suit, one of c d h s for Clubs, Diamonds, Hearts, and Spades. For example, Xh is the Ten of Hearts and As is the Ace of Spades.

Output

For each instance, print a single line with the indices of every winning player — those holding the strongest hand — in ascending order, separated by single spaces.

Rules

Rules of Texas Hold'em Poker

The five community cards are shared by all players, and each player also holds two private cards. Every player therefore has seven cards available, from which the best possible five-card hand is chosen. No more than five cards ever count as a hand: if, for example, all players form the same hand using all five community cards, the two private cards do not matter.

The hands are the usual poker hands, listed from strongest to weakest:

  • Royal Flush — a Flush that is also a Straight.
  • Poker — also known as Four of a Kind: four cards of the same rank.
  • Full House — a Three and a Pair together.
  • Flush — five cards of the same suit.
  • Straight — five consecutive cards (such as 7, 8, 9, X, J). The Ace may be the highest or the lowest card, but not both: A, 2, 3, 4, 5 and X, J, Q, K, A are Straights, but Q, K, A, 2, 3 is not.
  • Three — three cards of the same rank.
  • Two Pairs — two separate pairs.
  • Pair — two cards of the same rank.
  • High Card — anything else (no two cards of the same rank and no Straight).

When two or more players make the same kind of hand, the following tie-breakers are applied in order:

  1. For a Full House, the higher Three wins (3, 3, Q, Q, Q > 9, 9, 9, K, K).
  2. A Straight in which the Ace plays low is weaker than any other Straight (5, 6, 7, 8, 9 > A, 2, 3, 4, 5).
  3. The higher cards that form the hand win (2, 2, 4, K, K > X, X, Q, Q, A), comparing the remaining cards of the hand if necessary (4, 4, 5, K, K > 3, 3, K, K, A).
  4. Otherwise, the highest card not used in the hand decides (2, 2, 6, X, A > 2, 2, X, Q, K; 2, 2, 6, X, A > 2, 2, 3, 4, A; and 3, 4, 7, 8, A > 8, X, J, Q, K).

If no tie-breaker applies, the hands are equally strong. In particular, all four suits are equal in strength.

Examples1

  1. Example 1

    Input
    3
    2s 9c Ad 4h Xs
    As Ac
    9h 7h
    Xh 6h
    2
    3s 4s 5s As Ad
    6h 7h
    7d 6c
    
    Expected output
    1
    1 2