The Minimum Slot Machine

A slot machine accepts coins of two types, 1 and 2 euros. The game begins by pressing a button that makes the machine arbitrarily jump to any one of n states. Each coin you insert makes the machine move to a new state which is determined by the type of coin and the present state (it may move to just the same state where it is). If you wait more than 5 seconds without inserting a coin the game ends and either you loose the coins you inserted or you win the double of what you spent, depending on whether the state the machine stays in at the end is a loose or a win state. To make you suffer more, after you insert a new coin, the machine tells you what would have happened if you had stopped instead of going on. For the sake of minimality, a machine cannot have two equivalent states. Two states are equivalent if no matter the sequence of coins you insert, the result is the same starting from one state or from the other. Notice that a sequence of zero coins is valid, although the victory in such a case is just moral. Given a slot machine, determine the sets of states that are equivalent and for each set delete all the states except the first in alphabetical order, which is taken as the valid representative of that identical behaviour set. Show these remaining states. Input The input file contains several test cases, each of them containing several lines as follows. The first line of the input contains the number n (integer format) of states. The states are labeled in alphabetic sequence, starting from A, and there are at most 26. So, if n = 6, the states are A-F. The next n lines contain four characters each, separated by single spaces, where the first is the name of a state, the second the state the machine moves into if a 1 euro coin is inserted, the third the state the machine moves into if a 2 euro coin is inserted, and the fourth a w if the state is a win state or a — if it is a loose state. Output For each test case, your program must output the states that remain, after deleting the equivalent states, in a single line, separated by single spaces. Sample Input 3 ACBw BAB- CCBw Sample Output AB