TopcoderArchive
Archive
Algorithm problem

AnimalsGame

Problem statement, definition, constraints, and public examples.

Problem Statement

There are N people sitting in a circle, numbered 1 to N in clockwise order. Player 1 starts by naming any animal. They then go around the circle in clockwise order, and each player takes a turn. Each player must name an animal which starts with the same letter that the previously named animal ends with. After player N takes his turn, it becomes player 1's turn. The first player who cannot name a valid animal loses. You are given a String[] animalsNamed, the i-th element of which is the i-th animal named during the game. If all the named animals are valid, that means the player whose turn was next lost the game. Otherwise, the first player who named invalid animal lost the game. Return the (1-based) index of the player who lost the game.

Definition

Class:
AnimalsGame
Method:
loser
Parameters:
String[], int
Returns:
int
Method signature:
int loser(String[] animalsNamed, int N)
(be sure your method is public)

Constraints

  • animalsNamed will contain between 1 and 50 elements, inclusive.
  • Each element of animalsNamed will contain between 2 and 20 characters, inclusive.
  • animalsNamed will contain only uppercase letters ('A'-'Z').
  • All elements of animalsNamed will be distinct.
  • N will be between 2 and 10, inclusive.

Examples

  1. {"RACCOON","NIGHTINGALE","EAGLE","OX"}
    2
    Returns: 2
    OX does not start with the letter E (which EAGLE ends with). Player 2 named OX, so he lost the game.
  2. {"WOLF","FOX","KOALA","APE","PIGEON"}
    3
    Returns: 3
    Player 3 lost because he named KOALA, which does not start with X. There were more animals named after that, but KOALA was the first invalid one.
  3. {"SKUNK","KANGAROO","OTTER","RAT","TIGER","RABBIT","TAPIR","REINDEER",
     "RHINOCEROS","SQUIRREL","LEOPARD","DOVE","EEL","LLAMA","ANT","TURKEY",
     "YAK","KITTEN","NARWHAL","LADYBIRD","DOG","GORILLA","APE","ECHIDNA"}
    10
    Returns: 5
    All the players named valid animals. Player 5 had the next turn, so he lost the game.
  4. {"FANTAIL","URUTU","UMBRETTE"}
    5
    Returns: 2
Back to all problems