Colorville

Time limit1sMemory limit128 MB

Problem

A simple color-matching game for children is played on a board that is a single row of colored squares. Every player has one piece, and all pieces begin at START, just before the first square.

Players take turns in a fixed order — Player 1, then Player 2, and so on — each drawing the next card from a deck. A card shows either one colored square or two colored squares of the same color. On their turn a player moves their piece forward:

  • If the card shows a single color, move forward to the next square of that color.
  • If the card shows two squares of the same color, move forward to the second such square of that color.
  • If no such square lies ahead, move forward to the last square of the board.

A player wins the instant their piece lands on the last square. It is possible for the whole deck to be drawn without anyone winning.

Colors are written as capital letters AZ. Here is an example board (the leftmost square is the START side, the rightmost square is the FINISH side):

RYGPBRYGBRPOP

Consider the deck R, B, GG, Y, P, B, P, RR. With 3 players the game proceeds as follows:

  • Player 1 draws R and moves to the 1st square.
  • Player 2 draws B and moves to the 5th square.
  • Player 3 draws GG and moves to the 8th square.
  • Player 1 draws Y and moves to the 2nd square.
  • Player 2 draws P and moves to the 11th square.
  • Player 3 draws B and moves to the 9th square.
  • Player 1 draws P and moves to the 4th square.
  • Player 2 draws RR and wins — there is no R ahead of the piece, so it advances to the last square.

Using the same board and deck with 2 players, Player 1 wins after 7 cards. With 4 players, no one wins after the deck of 8 cards is exhausted.

Input

The input contains one or more games. Each game starts with a line of three integers: the number of players (1–4), the number of squares on the board (1–79), and the number of cards in the deck (1–200). The next line is a string of capital letters listing the board's square colors, one letter per square, from the START side to the FINISH side. The following lines are the deck — one card per line, in the order they are drawn — where each card is a single capital letter or the same capital letter written twice.

The input ends with a line whose first value is 0 (zero players); the other two values on that final line are present but meaningless.

Output

For each game, print exactly one line:

  • If a player reaches the last square, print Player X won after N cards., where X is the winning player's number and N is the total number of cards drawn (including the winning card).
  • If the deck is exhausted with no winner, print No player won after N cards., where N is the number of cards in the deck.

Always use the plural word "cards", even when N is 1.