Two players are playing a series of games of Rock–paper–scissors. There are a total of K games played. Player 1 has a sequence of moves denoted by string A and similarly player 2 has string B. If any player reaches the end of their string, they move back to the start of the string. The task is to count the number of games won by each of the player when exactly K games are being played.
Input: k = 4, a = “SR”, b = “R”
Output: 0 2
Game 1: Player1 = S, Player2 = R, Winner = Player2
Game 2: Player1 = R, Player2 = R, Winner = Draw
Game 3: Player1 = S, Player2 = R, Winner = Player2
Game 4: Player1 = R, Player2 = R, Winner = Draw
Input: k = 3, a = “S”, b = “SSS”
Output: 0 0
All the games are draws.
Approach: Let length of string a be n and length of string b be m. The observation here is that the games would repeat after n * m moves. So, we can simulate the process for n * m games and then count the number of times it gets repeated. For the remaining games, we can again simulate the process since it would be now smaller than n * m. For example, in the first example above, n = 2 and m = 1. So, the games will repeat after every n * m = 2 * 1 = 2 moves i.e. (Player2, Draw), (Player2, Draw), ….., (Player2, Draw).
Below is the implementation of the above approach:
Time Complexity: O(N * M)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Design a Chess Game
- Predict the winner in Coin Game
- Minimum possible final health of the last monster in a game
- Find the kth element in the series generated by the given N ranges
- Check if frequency of characters are in Recaman Series
- Number of ways to calculate a target number using only array elements
- Find a number which give minimum sum when XOR with every number of array of integers
- Smallest number dividing minimum number of elements in the array | Set 2
- Number of ways to split a binary number such that every part is divisible by 2
- Largest number dividing maximum number of elements in the array
- Smallest number dividing minimum number of elements in the Array
- Count number of ways to divide a number in 4 parts
- Represent a number as a sum of maximum possible number of Prime Numbers
- Minimum number of cubes whose sum equals to given number N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.