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

📅  最后修改于: 2021-09-24 04:58:34             🧑  作者: 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


Javascript


输出:
Player 2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程