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.

**Examples:**

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:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns 1 if first player wins, ` `// 0 in case of a draw and -1 if second player wins ` `int` `compare(` `char` `first, ` `char` `second) ` `{ ` ` ` `// If both players have the same move ` ` ` `// then it's a draw ` ` ` `if` `(first == second) ` ` ` `return` `0; ` ` ` ` ` `if` `(first == ` `'R'` `) { ` ` ` `if` `(second == ` `'S'` `) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `-1; ` ` ` `} ` ` ` `if` `(first == ` `'P'` `) { ` ` ` `if` `(second == ` `'R'` `) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `-1; ` ` ` `} ` ` ` `if` `(first == ` `'S'` `) { ` ` ` `if` `(second == ` `'P'` `) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `-1; ` ` ` `} ` `} ` ` ` `// Function that returns the count of games ` `// won by both the players ` `pair<` `int` `, ` `int` `> countWins(` `int` `k, string a, string b) ` `{ ` ` ` `int` `n = a.length(); ` ` ` `int` `m = b.length(); ` ` ` `int` `i = 0, j = 0; ` ` ` ` ` `// Total distinct games that can be played ` ` ` `int` `moves = n * m; ` ` ` `pair<` `int` `, ` `int` `> wins = { 0, 0 }; ` ` ` `while` `(moves--) { ` ` ` `int` `res = compare(a[i], b[j]); ` ` ` ` ` `// Player 1 wins the current game ` ` ` `if` `(res == 1) ` ` ` `wins.first++; ` ` ` ` ` `// Player 2 wins the current game ` ` ` `if` `(res == -1) ` ` ` `wins.second++; ` ` ` `i = (i + 1) % n; ` ` ` `j = (j + 1) % m; ` ` ` `} ` ` ` ` ` `// Number of times the above n * m games repeat ` ` ` `int` `repeat = k / (n * m); ` ` ` ` ` `// Update the games won ` ` ` `wins.first *= repeat; ` ` ` `wins.second *= repeat; ` ` ` ` ` `// Remaining number of games after ` ` ` `// removing repeated games ` ` ` `int` `rem = k % (n * m); ` ` ` `while` `(rem--) { ` ` ` `int` `res = compare(a[i], b[j]); ` ` ` ` ` `// Player 1 wins the current game ` ` ` `if` `(res == 1) ` ` ` `wins.first++; ` ` ` ` ` `// Player 2 wins the current game ` ` ` `if` `(res == -1) ` ` ` `wins.second++; ` ` ` `i = (i + 1) % n; ` ` ` `j = (j + 1) % m; ` ` ` `} ` ` ` `return` `wins; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `k = 4; ` ` ` `string a = ` `"SR"` `, b = ` `"R"` `; ` ` ` `auto` `wins = countWins(k, a, b); ` ` ` `cout << wins.first << ` `" "` `<< wins.second; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function that returns 1 if first ` `# player wins, 0 in case of a draw ` `# and -1 if second player wins ` `def` `compare(first, second): ` ` ` ` ` `# If both players have the same ` ` ` `# move then it's a draw ` ` ` `if` `(first ` `=` `=` `second): ` ` ` `return` `0` ` ` ` ` `if` `(first ` `=` `=` `'R'` `): ` ` ` `if` `(second ` `=` `=` `'S'` `): ` ` ` `return` `1` ` ` `else` `: ` ` ` `return` `-` `1` ` ` ` ` `if` `(first ` `=` `=` `'P'` `): ` ` ` `if` `(second ` `=` `=` `'R'` `): ` ` ` `return` `1` ` ` `else` `: ` ` ` `return` `-` `1` ` ` ` ` `if` `(first ` `=` `=` `'S'` `): ` ` ` `if` `(second ` `=` `=` `'P'` `): ` ` ` `return` `1` ` ` `else` `: ` ` ` `return` `-` `1` ` ` `# Function that returns the count ` `# of games won by both the players ` `def` `countWins(k, a, b): ` ` ` ` ` `n ` `=` `len` `(a) ` ` ` `m ` `=` `len` `(b) ` ` ` `i ` `=` `0` ` ` `j ` `=` `0` ` ` ` ` `# Total distinct games that ` ` ` `# can be played ` ` ` `moves ` `=` `n ` `*` `m ` ` ` `wins ` `=` `[ ` `0` `, ` `0` `] ` ` ` ` ` `while` `(moves > ` `0` `): ` ` ` `res ` `=` `compare(a[i], b[j]) ` ` ` ` ` `# Player 1 wins the current game ` ` ` `if` `(res ` `=` `=` `1` `): ` ` ` `wins[` `0` `] ` `+` `=` `1` ` ` ` ` `# Player 2 wins the current game ` ` ` `if` `(res ` `=` `=` `-` `1` `): ` ` ` `wins[` `1` `] ` `+` `=` `1` ` ` ` ` `i ` `=` `(i ` `+` `1` `) ` `%` `n ` ` ` `j ` `=` `(j ` `+` `1` `) ` `%` `m ` ` ` `moves ` `-` `=` `1` ` ` ` ` `# Number of times the above ` ` ` `# n * m games repeat ` ` ` `repeat ` `=` `k ` `/` `/` `(n ` `*` `m) ` ` ` ` ` `# Update the games won ` ` ` `wins[` `0` `] ` `*` `=` `repeat ` ` ` `wins[` `1` `] ` `*` `=` `repeat ` ` ` ` ` `# Remaining number of games after ` ` ` `# removing repeated games ` ` ` `rem ` `=` `k ` `%` `(n ` `*` `m) ` ` ` `while` `(rem > ` `0` `): ` ` ` `res ` `=` `compare(a[i], b[j]) ` ` ` ` ` `# Player 1 wins the current game ` ` ` `if` `(res ` `=` `=` `1` `): ` ` ` `wins[` `0` `] ` `+` `=` `1` ` ` ` ` `# Player 2 wins the current game ` ` ` `if` `(res ` `=` `=` `-` `1` `): ` ` ` `wins[` `1` `] ` `+` `=` `1` ` ` ` ` `i ` `=` `(i ` `+` `1` `) ` `%` `n ` ` ` `j ` `=` `(j ` `+` `1` `) ` `%` `m ` ` ` ` ` `return` `wins ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `k ` `=` `4` ` ` `a ` `=` `"SR"` ` ` `b ` `=` `"R"` ` ` ` ` `wins ` `=` `countWins(k, a, b); ` ` ` ` ` `print` `(wins[` `0` `], wins[` `1` `]) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

0 2

**Time Complexity:** O(N * M)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest odd divisor Game to check which player wins
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Count the number of walks of length N where cost of each walk is equal to a given number
- Design a Chess Game
- Predict the winner in Coin Game
- Minimum possible final health of the last monster in a game
- Expectimax Algorithm in Game Theory
- Implementation of Tic-Tac-Toe for 2 person game (User vs. User)
- Count the number of pop operations on stack to get each element of the array
- Number of comparisons in each direction for m queries in linear search
- Maximum number of nodes which can be reached from each node in a graph.
- Find number of segments covering each point in an given array
- Number of steps to sort the array by changing order of three elements in each step
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Pair with minimum absolute difference after solving each query
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Pairs of strings which on concatenating contains each character of "string"
- Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
- Find parent of each node in a tree for multiple queries
- Find the sum of remaining sticks after each iterations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.