#P9876. Will Prof. Pang Be Happy?
Will Prof. Pang Be Happy?
Will Prof. Pang Be Happy?
Professor Pang is playing a card game with his two friends Alice and Bob using a standard 52‐card deck. In a standard deck there are 13 ranks (A, K, Q, J, 10, 9, …, 2) in each of the four suits. The suits are irrelevant when comparing cards; only the ranks matter. The ranking is defined from high to low as follows:
\(A > K > Q > J > 10 > 9 > \cdots > 2\).
No card is drawn more than once. Initially, Alice and Bob are each dealt one or more cards while Prof. Pang is dealt exactly one card. All players can see every card in every hand.
The game proceeds in rounds with the following rules:
- The initiative player (the one who leads the round) chooses one card from his/her hand to start the round.
- Then, in turn, the other players may either pass or play a card. When playing a card, its rank must be strictly higher than every card played so far in the current round.
- The round ends when two players pass consecutively. The player who played the last card becomes the initiative player in the next round.
- If any player plays out all of his/her cards, the game terminates immediately.
In the playing order the roles are fixed. Initially, Alice is the initiative player; after her the turn order is Bob then Prof. Pang, and then it cycles back to Alice.
Each player has his/her own objective. Prof. Pang will be happy if and only if he is the first to empty his hand. Alice, who really likes milk tea, wants Prof. Pang to be happy so that after the game she can get a free cup of milk tea from him. Bob, however, wants to prevent Prof. Pang from winning. All players play optimally for their own interests.
Your task is: given the initial hands of the three players (cards are represented by their ranks only), determine whether Prof. Pang will be happy at the end of the game. It turns out that the outcome hinges on a critical observation. Since Prof. Pang holds exactly one card and his move is to play that card at some legal moment, his victory (i.e. being the first to empty his hand) can be forced if and only if Alice (who cooperates with him) can choose an opening card that prevents Bob from getting an opportunity to win. More precisely, let \(p\) be the rank value of Prof. Pang's card (using the mapping below) and let \(m\) be the maximum rank value among Bob's cards. (The rank values are as follows using natural numbers:
2 = 2, 3 = 3, 4 = 4, 5 = 5, 6 = 6, 7 = 7, 8 = 8, 9 = 9, 10 = 10, J = 11, Q = 12, K = 13, A = 14.)
Prof. Pang can force a win if and only if the following conditions are both satisfied:
[ p > m \quad\text{and}\quad \exists a \in A:; m \le a < p, ]
where \(A\) is the multiset of cards in Alice's hand (using their numeric values). In other words, if Alice has a card whose value is at least \(m\) (so that Bob cannot play after it, because playing requires a strictly higher rank) but still strictly lower than \(p\) (so that Prof. Pang can later play his card), then under optimal play Prof. Pang will eventually be able to play his card first and be happy.
If these conditions do not hold, then Bob can force a situation in which he wins (or at least prevents Prof. Pang from winning), leaving Prof. Pang unhappy.
Given the initial hands, output Yes
if Prof. Pang will be happy and No
otherwise.
inputFormat
The input consists of three lines:
- The first line contains Alice's cards, represented as space‐separated strings (each is one of: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A).
- The second line contains Bob's cards in the same format (one or more cards).
- The third line contains Prof. Pang's single card.
You may assume that the cards are drawn from a standard deck (so while there may be duplicate ranks, the total cards across all players are distinct cards drawn from a 52-card deck).
outputFormat
Output a single line: Yes
if Prof. Pang will be happy (i.e. he is the first to empty his hand under optimal play), or No
otherwise.
sample
K
Q
A
Yes