📌  相关文章
📜  找到重复删除第一个字符以清空给定字符串的游戏的获胜者

📅  最后修改于: 2021-04-27 23:34:56             🧑  作者: Mango

给定一个正整数N (代表游戏玩家的数量)和一个字符串arr []数组,该字符串数组由数字字符串组成,这些数字字符串由[‘1’,’N’]范围内的数字组成。考虑给i玩家分配了字符串arr [i] ,任务是在所有N个玩家按照以下规则最佳玩游戏时找到游戏的获胜者:

  • 玩家1开始游戏,删除字符串arr [1] (基于1的索引)的第一个字符,例如X ,然后在下一回合中,X玩家将玩游戏并删除arr [X]的第一个字符,然后很快。
  • 无法从分配的字符串删除任何字符的玩家将赢得比赛。

例子:

方法:使用Queue可以有效地删除每个字符串的第一个字符,可以解决该问题。请按照以下步骤解决问题:

  • 初始化一个队列数组,例如Q [] ,以使Q [i]存储字符串arr [i]的字符。
  • 根据游戏规则使用变量i遍历数组Q []并检查Q [i]中的字符数是否为0 。如果发现是真的,则打印“ Player i”

下面是上述方法的实现:

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


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
 
// Function to find the winner of a game of
// repeatedly removing the first character
// to empty a String
static void find_Winner(String[] arr, int N)
{
 
    // Store characters of each
    // String of the array arr[]
    @SuppressWarnings("unchecked")
    Vector [] Q = new Vector[N];
    for (int i = 0; i < Q.length; i++)
        Q[i] = new Vector();
   
    // Stores count of Strings in arr[]
    int M = arr.length;
 
    // Traverse the array arr[]
    for (int i = 0; i < M; i++)
    {
 
        // Stores length of current String
        int len = arr[i].length();
 
        // Traverse the String
        for (int j = 0; j < len; j++)
        {
 
            // Insert arr[i][j]
            Q[i].add(arr[i].charAt(j));
        }
     
    }
 
    // 1st Player starts the game
    int player = 0;
 
    while (Q[player].size() > 0 )
    {
 
        // Stores the player number
        // for the next turn
        int nextPlayer
            = Q[player].get(0) - '0'-1;
 
        // Remove 1st character of
        // current String
        Q[player].remove(0);
 
        // Update player number for
        // the next turn
        player = nextPlayer;
    }
    System.out.print("Player " +  (player + 1));
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    String[] arr
        = { "323", "2", "2" };
    find_Winner(arr, N);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program to implement
# the above approach
 
# Function to find the winner of a game of
# repeatedly removing the first character
# to empty a string
def find_Winner(arr, N) :
  
    # Store characters of each
    # string of the array arr[]
    Q = [0]*N  
    for i in range(N) :       
        Q[i] = []
  
    # Stores count of strings in arr[]
    M = len(arr)
  
    # Traverse the array arr[]
    for i in range(M) :
  
        # Stores length of current string
        Len = len(arr[i])
  
        # Traverse the string
        for j in range(Len) :
  
            # Insert arr[i][j]
            Q[i].append(ord(arr[i][j]) - 1)
  
    # 1st Player starts the game
    player = 0
  
    while (len(Q[player]) > 0) :
  
        # Stores the player number
        # for the next turn
        nextPlayer = Q[player][0] - ord('0')
  
        # Remove 1st character of
        # current string
        del Q[player][0]
  
        # Update player number for
        # the next turn
        player = nextPlayer
  
    print("Player", (player + 1))
     
N = 3
arr = [ "323", "2", "2" ]
 
find_Winner(arr, N)
 
# This code is contributed by divyeshrabadiya07.


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the winner of a game of
// repeatedly removing the first character
// to empty a String
static void find_Winner(String[] arr, int N)
{
     
    // Store characters of each
    // String of the array []arr
    List [] Q = new List[N];
    for(int i = 0; i < Q.Length; i++)
        Q[i] = new List();
         
    // Stores count of Strings in []arr
    int M = arr.Length;
 
    // Traverse the array []arr
    for(int i = 0; i < M; i++)
    {
         
        // Stores length of current String
        int len = arr[i].Length;
 
        // Traverse the String
        for(int j = 0; j < len; j++)
        {
             
            // Insert arr[i,j]
            Q[i].Add(arr[i][j]);
        }
    }
 
    // 1st Player starts the game
    int player = 0;
 
    while (Q[player].Count > 0 )
    {
         
        // Stores the player number
        // for the next turn
        int nextPlayer = Q[player][0] - '0'- 1;
         
        // Remove 1st character of
        // current String
        Q[player].RemoveAt(0);
 
        // Update player number for
        // the next turn
        player = nextPlayer;
    }
    Console.Write("Player " + (player + 1));
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 3;
    String[] arr = { "323", "2", "2" };
     
    find_Winner(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


输出:
Player 2

时间复杂度: O(N * M),其中M是数组中存在的最长字符串的长度。
辅助空间: O(N * M)