TopCoder problem "TennisSet" used in SRM 183 (Division I Level One)



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).

 

Definition

    
Class:TennisSet
Method:firstSet
Parameters:String[]
Returns:String
Method signature:String firstSet(String[] points)
(be sure your method is public)
    
 

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.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=2339

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4735&pm=2339

Writer:

dgoodman

Testers:

lbackstrom , brett1479

Problem categories:

Simulation