In rock-paper-scissors two players show a move at the same time in every round: Rock, Paper, or Scissors. Equal moves are a draw. Otherwise Rock beats Scissors, Paper beats Rock, and Scissors beats Paper.
In this problem two finite state machines repeat this game forever. Formally, machine here means a Moore machine.
A machine that plays rock-paper-scissors has finitely many states. Each state fixes two things: the move shown in the coming round, and the state the machine moves to when its opponent shows Rock, Paper, or Scissors. Both machines learn the opponent's move only after the round is over.
You know the whole design of the opponent machine. One thing is missing: you do not know which state it starts in. Your machine still has to beat it in at least 99% of the first 109 rounds. That is what we call an epic win.
Many machines reach that target, so this problem pins down one of them. Build your machine by the procedure below and print it.
Number the opponent states from 1 to n. Write move(q) for the move shown in state q, and next(q,x) for the opponent state reached from q when you show x.
Separation distance. For an ordered pair (a,b) of opponent states define e(a,b) as follows. If move(a)=move(b) then e(a,b)=1. Otherwise e(a,b)=1+minxe(next(a,x),next(b,x)) over the three moves x. If no choice of your moves ever brings a round where the two states show different moves, then e(a,b)=∞. Every pair with a=b is ∞.
Candidate sets. A candidate set is a nonempty set of opponent states. Reduce a candidate set B by this rule: if e(a,b)=∞ for every two distinct a,b∈B, replace B by the set holding only the smallest element of B, and otherwise keep B unchanged. One reduced candidate set is one state of your machine.
The move shown in a reduced set B is this.
The transitions of B are this. Let x be the move chosen above and let m be the observed opponent move. Put Bm={q∈B:move(q)=m}. If Bm is empty, the transition on m goes to state 1. Otherwise it goes to the reduced form of {next(q,x):q∈Bm}.
Number the states in this order. State 1 is the reduced form of the whole set {1,2,…,n}. Handle the states from the smallest number upwards, and inside one state go through the observed moves in the order Rock, Paper, Scissors. Every time a set without a number shows up, give it the smallest free number. Equal sets always get the same number. Print the states in that order.
The first line has one integer n, the number of states of the opponent machine (1≤n≤100). States are numbered from 1 to n.
Each of the next n lines describes one state. Line i has a character ci and integers ri, pi, si. The character ci is R, P, or S and is the move shown in state i. The integers ri, pi, si are the states the opponent moves to from state i when you show Rock, Paper, or Scissors (1≤ri,pi,si≤n).
On the first line print k, the number of states of your machine.
On each of the next k lines print one state in the same format as the input: the move shown in that state as R, P, or S, then the states it moves to when the observed opponent move is Rock, Paper, or Scissors.
State 1 is the starting state of your machine. On every input of this problem the procedure above produces at most 50000 states.
Collect every opponent state that agrees with what you have observed through round t, and you get the current candidate set of your machine. The two differ only where indistinguishable states were folded into one. So once the candidate set holds a single state you know every move the opponent will show and you win every remaining round.
In each round either the candidate set gets smaller or the separation distance of the chosen pair drops by one. That distance never exceeds n−1 and the set can shrink at most n−1 times, so fewer than n2 rounds are not won. That is far under 1% of 109.