Connection Status:
Competition Arena > TennisSet
SRM 183 · 2004-02-11 · by dgoodman · Simulation
Class Name: TennisSet
Return Type: String
Method Name: firstSet
Arg Types: (vector<string>)
Problem Statement

Problem Statement

In tennis, the two players play games, alternating which player is the server in each game. When a player has won at least 4 points and has won at least 2 more points than his opponent, he has won the game. When a player has won at least 6 games and has won at least 2 more games than his opponent, he has won the set.

We have a String[] points that tells us the result of each point in a recent tennis match. Each character is either 'S' indicating that the server won that point, or an 'R' indicating that the returner (the non-server) won that point. There is no significance to the breaks between individual Strings in points. All together they represent one sequence of points played in the match.

Call the player who serves in the first game Player A and his opponent Player B. We want to know the score in the first set, with the number of games won by Player A, then a hyphen ('-'), then the number of games won by Player B. The data in points may go beyond the end of the first set, in which case the extra data can be ignored. The first set may not yet be completed, in which case we want the score to include only the completed games.

Create a class TennisSet that contains a method firstSet that is given String[] points and returns the score of the first set as a String. The returned String must not contain any extra characters (such as spaces or leading zeros).

Notes

  • We are NOT using the tiebreaker which is sometimes used when a set reaches 6-6.

Constraints

  • points will contain between 1 and 50 elements inclusive.
  • Each element of points will contain between 1 and 50 characters inclusive.
  • Each element of points will contain only 'S' and 'R'.
Examples
0)
{"SSSSSSSSSSSSSSS"}
Returns: "2-1"

The first game was won by Player A in four points. Then B served the second game and won it in four points. Then A served again and won 4 points in a row. The fourth game is still in progress (with B ahead by 3 points).

1)
{"SS","SRSS","RRRRR"}
Returns: "2-0"

Player A won the first game 4 to 1. As receiver in the second game, A also won 4 points to 1 point. Only one point has been played in the third set so far.

2)
{"SSSSRRRRSSSSRR","RRSSSSRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR","R"}
Returns: "6-0"

Player A won all the games without losing a single point. There is a lot of extra data that applies to other sets and is ignored.

3)
{"SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS","SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS","SSSSSSSSSSSSSSSSSSSSSS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SSSSSSSSSSSSSSSSSSSS","SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS","SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS","SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS","SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS","SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS","SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS","SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS"}
Returns: "313-312"
4)
{"SSSRRRRSSRRRRRSSSSRRRRSSSSRRRRSSSRRRSRSRSRS","R","SRSRSRSRSRSRSRSRS"}
Returns: "0-5"

Submissions are judged against all 34 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class TennisSet with a public method string firstSet(vector<string> points) · 34 test cases · 2 s / 256 MB per case

Submitting as anonymous