Rather Perplexing Showdown (Small)
InterviewTime limit5sMemory limit512 MB
Find the alphabetically smallest left-to-right lineup of R rocks, P papers, and S scissors so that a single-elimination bracket never pairs identical moves.
- Level
Medium6 of 10
- Topics
- Divide and conquer, Recursion, Greedy, String
- Solved
- No attempts yet
Problem
You've been asked to organize a Rock-Paper-Scissors tournament. The tournament has a single-elimination format and runs for rounds; players participate.
Initially, the players are lined up from left to right in some order that you specify. In each round, the first and second players in the lineup (starting from the left) play a match against each other, the third and fourth players in the lineup (if they exist) play a match against each other, and so on; all of these matches occur simultaneously. The winners of these matches remain in the lineup, in the same relative order, and the losers leave the lineup and go home. Then a new round begins. This continues until only one player remains in the lineup; that player is declared the winner.
In each Rock-Paper-Scissors match, each of the two players secretly chooses one of Rock, Paper, or Scissors, and then they compare their choices. Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. If one player's choice beats the other player's choice, then that player wins and the match is over. However, if the players make the same choice, then it is a tie, and they must choose again and keep playing until there is a winner.
The players this year are stubborn and not very strategic. Each one has a preferred move and plays only that move in every match, regardless of what the opponent does. Because of this, if two players with the same move go up against each other, they keep tying and their match goes on forever. If this happens, the tournament never ends and you become a laughingstock.
This year, there are players who prefer Rock, players who prefer Paper, and players who prefer Scissors. Knowing this, you want to create a lineup that guarantees that the tournament goes to completion and produces a single winner, that is, no match is ever a tie. Your boss has asked you to produce a list of all such lineups (written in left to right order, with R, P, and S standing for players who prefer Rock, Paper, and Scissors, respectively), and then put that list in alphabetical order.
Your boss will lazily pick the first lineup on the list. What will that be? Or do you have to tell your boss that it is IMPOSSIBLE to prevent a tie?
Input
The first line of the input gives the number of test cases, . lines follow; each represents one test case. Each test case consists of four integers: , , , and , as described in the statement above.
Limits
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is either IMPOSSIBLE or a string of length representing the alphabetically earliest starting lineup that solves the problem. Every character in a lineup must be R, P, or S, and there must be Rs, Ps, and Ss.
Hint
In sample case #1, there are only two players and the tournament consists of one round. It doesn't matter what order the two line up in; the Paper player defeats the Rock player. You give your boss the alphabetically ordered list PR, RP, and the first element is PR.
In sample case #2, the only two players both play Rock, so a tie is unavoidable.
In sample case #3, there are four players and the tournament goes on for two rounds. In the first round, the first player (Paper) loses to the second player (Scissors), and the third player (Rock) defeats the fourth player (Scissors). The second round lineup is PR, and the first remaining player (Paper) defeats the other remaining player (Rock), so the tournament ends with a winner and no ties.
Here is an illustration of the tournament for sample case #3:

Other lineups such as PSSR appear on the list you give to your boss, but PSRS is alphabetically first.
In sample case #4, the only way to organize the first round with no ties is to create two matches with one Rock player and one Scissors player each. But both of those matches have a Rock winner, and when these two winners face each other, there is a tie.