Rhinoceros Beetle#4158

Rhinoceros Beetle

1 second
64 megabytes
Medium

Welcome to the grand competition of the Arthropoda Card Marathon, where players gather for a challenging round of Texas Hold'em Poker. In this tournament, participants aim to build the strongest poker hand from their available cards. Can you write a program to determine which players are the winners in each round?

Input

The input consists of several instances. The first line of each instance contains one integer (1 \le N \le 10), the number of players. The second line contains exactly five cards separated by a space — these are the Texas Hold'em "community" cards shared by all the players. Each of the following N lines contains two cards separated by a space — the cards held by individual players, with the first such line for Player 1, the second for Player 2, and so on.

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

Output

For each input instance, output a single line containing the numbers of all winners in ascending order, separated by a space. Output the ordered sequence of indexes of players that hold the strongest hand for the given configuration of cards.

Rules of Texas Hold'em Poker

Five cards (the community cards) are shared by all the players and each player holds private cards that may be combined with the community cards, meaning that each player has 7 cards available, out of which cards are chosen to make the best possible hand.

Note that no more than cards can be counted as a hand under any circumstances. For instance, if all players make the same hand using all community cards, the two cards they are holding do not count.

The hands in Texas Hold'em Poker are the usual poker hands ("figures"). Listed from strongest to weakest figure, these are:

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

5 consecutive cards (such as 7, 8, 9, X, J), where Ace may be the highest or lowest card, but not both — for example, A, 2, 3, 4, 5 or X, J, Q, K, A are Straights, but Q, K, A, 2, 3 is not

Three

3 cards of the same rank

Two pairs

two pairs of cards of the same rank

Pair

two cards of the same rank

One card

anything else (no two cards of the same rank, and no Straight)

If two or more players have the same figure, the following tie-breaker rules are applied, in order:

  1. With Full House, the highest card of the Three wins (3, 3, Q, Q, Q > 9, 9, 9, K, K).

  2. If Ace plays low in a Straight, it is weaker than other Straights (5, 6, 7, 8, 9 > A, 2, 3, 4, 5).

  3. The highest card of the figure wins (2, 2, 4, K, K > X, X, Q, Q, A), or the other cards of the figure if necessary (4, 4, 5, K, K > 3, 3, K, K, A).

  4. Otherwise, the highest card that is not used in the figure 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 can be used, both hands have the same strength. Specifically, all four suits have the same strength.

Examples

Example 1

Loading
Loading