Statistics

Problem Statement for "Tourney"

Problem Statement

In a single-elimination tournament, the schedule is determined by placing the teams in a vertical list called "the tournament bracket". In the first round the top two teams in the bracket play each other, then the next two, etc. In the second round the top two remaining teams play each other, then the next two, etc. This continues until there is only one team left -- the tournament winner.

The number of teams in the tournament should be a power of 2 -- if it isn't, byes are added to the bracket. Byes are never placed so that they will "play" each other. When a team's opponent is "bye", there is no game played and the team automatically advances as if it had won a game. Here is an example with a bracket of length 4 containing one bye (see Example 0).

             ROUND 1       ROUND 2
      Duke
          |--------- Duke
      UCLA              |
                        |-------- MIT
      bye               |
          |---------- MIT
      MIT

We have the tournament bracket (including any byes) from last year's tournament. We also have a list of the results of the games in the order in which they were played. We want to figure out who won last year's tournament. Create a class Tourney that contains a method winner that is given a String[] bracket and a String results and that returns the name of the winning team as a String. The bracket is given in order from top to bottom, and the results are given as a string of 'H' or 'L' indicating for each game (in the order in which the games were played) whether the Higher or Lower team from the bracket won that game. results contains a character for each real game, but not for byes.

Definition

Class:
Tourney
Method:
winner
Parameters:
String[], String
Returns:
String
Method signature:
String winner(String[] bracket, String results)
(be sure your method is public)

Notes

  • it is possible that two teams with the same name could be in the tournament.

Constraints

  • the number of elements in bracket will be a power of 2 between 2 and 32 inclusive
  • each element in bracket will have length between 1 and 50 inclusive
  • each element in bracket will contain only uppercase characters 'A'-'Z', or will be "bye"
  • no two bye elements will be placed such that they would play each other
  • the length of results will be exactly one less than the number of non-bye elements in bracket
  • each character of results will be uppercase 'H' or uppercase 'L'

Examples

  1. {"DUKE","UCLA","bye","MIT"}

    "HL"

    Returns: "MIT"

    DUKE played UCLA in the first round and MIT advanced with a bye. DUKE won since the first game was won by the team higher on the bracket. In the second round, MIT beat DUKE since the second game was won by the lower team.

  2. {"A","B","C","bye","D","E","F","bye"}

    "LHHLH"

    Returns: "B"

    Round 1: B beat A, D beat E Round 2: B beat C, F beat D Round 3: B beat F

  3. {"A","B","A","C","X","bye","bye","D", "E","F","G","H","I","J","K","L"}

    "HLHLHLHLHLHLH"

    Returns: "A"

  4. {"A","B","A","C","X","bye","bye","D", "E","F","G","H","I","J","K","L"}

    "HHHHHHHHHHHHH"

    Returns: "A"

  5. {"A","B","A","C","X","bye","bye","D", "E","F","G","H","I","J","K","L"}

    "LHHLLHHLHLLLH"

    Returns: "D"

  6. {"MIT","bye"}

    ""

    Returns: "MIT"

  7. {"A","bye","A","bye","A","bye","A","bye","A","bye","A","bye","A","bye","A","bye", "A","bye","A","bye","A","bye","A","bye","A","bye","A","bye","A","bye","X","bye"}

    "LLLLLLLLLLLLLLL";

    Returns: "X"

  8. {"P","Q","R","S","T","U","V","W","X","Y","Z","HARVARD","A","B","C","D", "P","Q","R","S","T","U","V","W","X","Y","Z","HARVARD","A","B","C","D"}

    "LLLLLLLLLLLLLLLLLLLLLLLLHHHHLLL"

    Returns: "HARVARD"

  9. {"ABLE","BUTTER","CHALK","bye","bye","NORTHERN","X","bye"}

    "HLHL"

    Returns: "NORTHERN"

  10. {"bye","ABCDEFGHIJKLM"}

    ""

    Returns: "ABCDEFGHIJKLM"

  11. {"WELL","bye","bye","MISS","AMERICAN","PIE","bye","CHEVY"}

    "HHHH"

    Returns: "WELL"

  12. {"ONE","TWO","THREE","FOUR"}

    "LHL"

    Returns: "THREE"

  13. {"A","B","A","C","X","bye","bye","D", "E","F","G","H","I","J","K","L"}

    "LHHLLHHHHHLLH"

    Returns: "X"

  14. {"STANFORD","bye","STANFORD","bye"}

    "L"

    Returns: "STANFORD"

  15. {"AUGUSTANA","DORDT"}

    "H"

    Returns: "AUGUSTANA"

  16. {"AUGUSTANA","DORDT"}

    "L"

    Returns: "DORDT"

  17. {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q", "R","S","T","U","V","W","X","Y","Z","AA","AB","AC","AD","AE","AF"}

    "LHLHLHHLHLLHLLLHLHHLHHLHLHHHHHL"

    Returns: "Q"

  18. {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q", "R","S","T","U","V","W","X","Y","Z","AA","AB","AC","AD","AE","AF"}

    "LHHHHLHLHLLHHLHLHHLHLLLLLLLLHLH"

    Returns: "E"

  19. { "bye", "MIT" }

    ""

    Returns: "MIT"

  20. { "A", "B", "C", "bye" }

    "LH"

    Returns: "B"

  21. { "DUKE", "BYE", "bye", "MIT" }

    "HL"

    Returns: "MIT"

  22. { "OIEJ", "PIERJPEIOWJ", "WOEIJFDS", "PWEJIOWE", "PWEOIJFEW", "OPFEJWPIOJE", "PEWFIOJPEFWJ", "PIEWURPWOWEPEOWIR", "PWEOIJUFDPSJ", "PWEORPEWOJ", "PEWOJPOEWJ", "OPWEIUJDPJV", "PWOIJEPOWEJ", "POIJEWTPWEJ", "PWEOITJPEWOIJTP", "WPOEITHJPEWTOJ" }

    "LLLLLLLLLLLLLLL"

    Returns: "WPOEITHJPEWTOJ"

  23. { "bye", "A" }

    ""

    Returns: "A"

  24. { "bye", "X" }

    ""

    Returns: "X"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: