Find the winner of game of repeatedly removing the first character to empty given string | C ++ program to implement the above approach ; Function to find the winner of a game of repeatedly removing the first character to empty a string ; Store characters of each string of the array arr [ ] ; Stores count of strings in arr [ ] ; Traverse the array arr [ ] ; Stores length of current string ; Traverse the string ; Insert arr [ i ] [ j ] ; 1 st Player starts the game ; Stores the player number for the next turn ; Remove 1 st character of current string ; Update player number for the next turn ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_Winner ( vector < string > & arr , int N ) { vector < queue < char > > Q ( N ) ; int M = arr . size ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int len = arr [ i ] . length ( ) ; for ( int j = 0 ; j < len ; j ++ ) { Q [ i ] . push ( arr [ i ] [ j ] - 1 ) ; } } int player = 0 ; while ( Q [ player ] . size ( ) > 0 ) { int nextPlayer = Q [ player ] . front ( ) - '0' ; Q [ player ] . pop ( ) ; player = nextPlayer ; } cout << " Player ▁ " << ( player + 1 ) ; } int main ( ) { int N = 3 ; vector < string > arr = { "323" , "2" , "2" } ; find_Winner ( arr , N ) ; return 0 ; }