You have seen a card magic trick named "Spelling Bee". The process goes as follows:
- The magician first arranges 13 cards in a circle, as shown in the figure below.
- Starting from the marked position, he counts the cards clockwisely, saying "A--C--E".
- He turns the card at the "E" position, and... it is an Ace!
- Next, he takes away the Ace and continues to count the cards, saying "T--W--O".
- He turns over the card at position "O"... it is a Two!!
- He continues to do this with the rest of the cards from Three to King. :-)
Now, how does the magician arrange the cards?
Input
Input consists of several test cases. Each case begins with an integer N (1 ≤ N ≤ 52), the number of cards to be used in the magic trick. The following N lines show the order of the turning-over of the cards and the words to be spelt. None of the words will have more than 20 characters. The format for each card is a string with two characters: first the value, and second the suit.
Input ends with a test case where N=0. This test case should not be processed.
Output
For each case, your program should output the initial arrangement of the cards.
Sample Input
13 AS ACE 2S TWO 3S THREE 4C FOUR 5C FIVE 6C SIX 7D SEVEN 8D EIGHT 9D NINE TH TEN JH JACK QH QUEEN KH KING 0
Sample Output
QH 4C AS 8D KH 2S 7D 5C TH JH 3S 6C 9D
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Main
{
public static final boolean DEBUG = false;
public BufferedReader cin;
public PrintWriter cout;
public StreamTokenizer tokenizer;
public int n;
public Card[] cards;
public boolean[] vis;
static class Card
{
String name, spelt;
}
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(new FileInputStream("e:\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
tokenizer = new StreamTokenizer(cin);
tokenizer.resetSyntax();
tokenizer.wordChars('a', 'z');
tokenizer.wordChars('A', 'Z');
tokenizer.wordChars('0', '9');
tokenizer.wordChars(128 + 32, 255);
tokenizer.whitespaceChars(0, ' ');
tokenizer.commentChar('/');
tokenizer.quoteChar('"');
tokenizer.quoteChar('\'');
cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}
public String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tokenizer.nval);
} else return tokenizer.sval;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean input()
{
n = Integer.parseInt(next());
if (n == 0) return false;
cards = new Card[n];
for (int i = 0; i < n; i++) {
cards[i] = new Card();
cards[i].name = next();
cards[i].spelt = next();
}
return true;
}
public int nextPos(int cur)
{
int i = cur;
while (vis[i]) i = (i + 1) % n;
return i;
}
public void solve()
{
String[] ans = new String[n];
vis = new boolean[n];
Arrays.fill(vis, false);
int cur = 0;
for (int i = 0; i < n; i++) {
int len = cards[i].spelt.length();
for (int j = 0; j < len; j++) {
cur = nextPos(cur);
cur = (cur + 1) % n;
}
int prev = (cur - 1 + n) % n;
ans[prev] = cards[i].name;
vis[prev] = true;
}
for (int i = 0; i < n; i++) {
if (i != 0) cout.print(" ");
cout.print(ans[i]);
}
cout.println();
cout.flush();
}
public static void main(String[] args)
{
Main solver = new Main();
solver.init();
while (solver.input()) {
solver.solve();
}
}
}