P16922 [JLCPC 2026] Straight
Description
Dou Dizhu is one of the most popular card games in China. The game uses a deck of 54 cards (52 normal cards + 2 jokers).
In Dou Dizhu, a *straight* is a hand type consisting of at least 5 cards with consecutive ranks. From low to high, the ranks are: 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A. Note that 2 and both jokers cannot appear in a straight.
Now tarjen has 17 cards in hand, and he really likes straights. The silly tarjen wants to know the length of the longest straight he can play, but he cannot count yet. Could you help him?
If he cannot play any straight, output $0$.
Input Format
The first line contains an integer $T$ ($1 \le T \le 1000$), the number of test cases. The next $T$ lines each describe one test case.
Each test case contains $17$ space-separated strings, representing the 17 cards. Each card is represented as follows:
- `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `J`, `Q`, `K`, `A`: a card of the corresponding rank;
- `2`: a rank 2 card;
- `SJ`: the small joker;
- `BJ`: the big joker.
It is guaranteed that each normal rank (`A`, `2`$\sim$`10`, `J`, `Q`, `K`) appears at most 4 times in each test case, and the small joker and big joker appear at most once each.
Output Format
For each test case, output one integer per line, the length of the longest straight that can be played. If no straight can be played (i.e., there is no consecutive rank sequence of length $\ge 5$), output $0$.
Explanation/Hint
In the first sample, the ranks available for making a straight are $3, 4, 5, 6, 7$ and A. The longest consecutive sequence is $3, 4, 5, 6, 7$, with length $5$.
In the second sample, there is no valid straight, so the answer is $0$.
Translated by ChatGPT 5