Problem C
Cribbage On Steroids

Cribbage is a two-person card game where players score points for various combinations of cards. A standard $52$-card deck is used where cards have one of $4$ suits (not important in this problem) and one of $13$ ranks. The card ranks, from lowest to highest, are Ace (A), $2$, $3$, $4$, $5$, $6$, $7$, $8$, $9$, $10$ (T), Jack (J), Queen (Q), King (K). In normal cribbage, players make combinations from a hand consisting of five cards (four in their hand and one common card). The possible combinations and their point values are the following:


: any combination of card ranks that total exactly $15$ scores $2$ points. The values of Kings, Queens and Jacks are $10$, the value of Aces is $1$ and all other card values are the same as their rank.


: any two cards with the same rank scores $2$ points. Note that three cards with the same rank will score $6$ points since it contains three separate pairs; four cards with the same rank scores $12$ points; five cards with the same rank$\ldots $ but we’re getting ahead of ourselves.


: for each disjoint run of length three or more, each set of cards that generates the longest run scores $1$ point per card. So for example, the cards $2,2,3,4,5,8,9$, T, J, Q will score $8$ points for two four-card sequences (there are $2$ ways to make $2,3,4,5$) and $5$ points for a five-card sequence (there is one way to make $8,9$, T, J, Q).

The total score across each of these three categories is the hand’s score.

For example, a five-card hand consisting of $4,5,5,5,6$ will score $23$ points: $8$ points for $15$’s (three $4$-$5$-$6$ combinations and one $5$-$5$-$5$), $6$ points for pairs (three pairs of $5$’s) and $9$ points for runs (three $4$-$5$-$6$ runs). The five-card hand T,T,J,Q,Q scores $16$: $4$ points for pairs (T’s and Q’s) and $12$ points for runs (four different runs of T-J-Q). There are other ways to score as well but the three rules above will suffice for this problem.

Now as we said, in normal cribbage you look for combinations in five-card hands. But we’re far from normal. Your task is to determine the value of an $n$-card hand when $n$ can be significantly larger than $5$.


Input starts with a integer $n$ ($5 \leq n \leq 100$) indicating the number of cards in the hand. Following this are $n$ characters taken from the set {A,2,3,4,5,6,7,8,9,T,J,Q,K} indicating the ranks of the $n$ cards. These characters will be on one or more lines with one space between any two characters on the same line. The card ranks will not necessarily be in sorted order. Note that unlike a standard deck of cards, there may be more than four cards of any rank.


Output the total score achieved by the $n$-card hand.

Sample Input 1 Sample Output 1
4 5 6 5 5
Sample Input 2 Sample Output 2
A 2 3 4 5
6 7 8 9 T
Sample Input 3 Sample Output 3
2 2 3 4 5 8 9 T J Q

Please log in to submit a solution to this problem

Log in