📌  相关文章
📜  确定从字符串中删除字符的游戏的获胜者

📅  最后修改于: 2021-05-04 15:43:56             🧑  作者: Mango

给定数字字符串str ,任务是确定当两个玩家根据给定条件使用该字符串以最佳方式玩游戏时,确定游戏的获胜者:

  • 玩家1总是最先开始。
  • 一轮回合中,一个玩家可以从字符串删除连续的元素,并且被删除的元素数量将总计为自己的分数。播放器1将删除奇数个连续元素,而播放器1将删除偶数个连续元素。
  • 任何无法采取行动的玩家都会输掉比赛。
  • 除去所有字符串后,得分最高的玩家将赢得比赛,如果得分相等,则显示“ -1”

例子:

方法:请按照以下步骤解决问题:

  1. 创建一个大小为10的数组A []以存储给定字符串中所有数字的频率。
  2. 迭代给定的字符串,并更新上述数组中每个数字的频率。
  3. 遍历数组A []并采用两个变量sum1 = 0sum2 = 0来存储分数总和。
  4. 如果索引为奇数,则增加sum1 ,即玩家1回合;否则,如果索引为偶数,则增加sum2 ,即玩家2回合。
  5. 在上述步骤之后,检查sum1是否等于sum2,然后打印-1,否则打印根据得分赢得游戏的玩家编号。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find the winner of the
// game when both players play optimally
void determineWinner(string str)
{
    // Stores the frequency of all digit
    vector A(10);
  
    // Stores the scores of player1
    // and player2 respectively
    int sum1 = 0, sum2 = 0;
  
    // Iterate to store frequencies
    for (int i = 0; i < str.length(); i++) {
        A[int(str[i]) - 48]++;
    }
  
    for (int i = 0; i <= 9; i++) {
  
        // Turn for the player1
        if (i % 2 != 0) {
  
            // Add score of player1
            sum1 = sum1 + A[i];
        }
        else {
  
            // Add score of player2
            sum2 = sum2 + A[i];
        }
    }
  
    // Check if its a draw
    if (sum1 == sum2) {
        cout << "-1";
    }
  
    // If score of player 1 is greater
    else if (sum1 > sum2) {
        cout << "Player 1";
    }
  
    // Otherwise
    else {
        cout << "Player 2";
    }
}
  
// Driver Code
int main()
{
    string str = "78787";
    determineWinner(str);
    return 0;
}


Java
// Java program for the above approach 
import java.util.*;
  
class GFG{
  
// Function to find the winner of the
// game when both players play optimally
static void determineWinner(String str)
{
      
    // Stores the frequency of all digit
    int[] A = new int[10];
  
    // Stores the scores of player1
    // and player2 respectively
    int sum1 = 0, sum2 = 0;
  
    // Iterate to store frequencies
    for(int i = 0; i < str.length(); i++)
    {
        A[(int)str.charAt(i) - 48]++;
    }
  
    for(int i = 0; i <= 9; i++)
    {
          
        // Turn for the player1
        if (i % 2 != 0) 
        {
  
            // Add score of player1
            sum1 = sum1 + A[i];
        }
        else
        {
  
            // Add score of player2
            sum2 = sum2 + A[i];
        }
    }
  
    // Check if its a draw
    if (sum1 == sum2) 
    {
        System.out.print("-1");
    }
  
    // If score of player 1 is greater
    else if (sum1 > sum2)
    {
        System.out.print("Player 1");
    }
  
    // Otherwise
    else 
    {
        System.out.print("Player 2");
    }
}
  
// Driver code
public static void main (String[] args)
{
    String str = "78787";
      
    determineWinner(str);
}
}
  
// This code is contributed by offbeat


Python3
# Python3 program for the above approach 
  
# Function to find the winner of the
# game when both players play optimally
def determineWinner(str):
  
    # Stores the frequency of all digit
    A = [0 for i in range(9)];
  
    # Stores the scores of player1
    # and player2 respectively
    sum1 = 0; sum2 = 0;
  
    # Iterate to store frequencies
    for i in range(0, len(str)):
        A[int(str[i])] += 1;
      
    for i in range(0, 9):
  
        # Turn for the player1
        if (i % 2 != 0):
  
            # Add score of player1
            sum1 = sum1 + A[i];
        else:
  
            # Add score of player2
            sum2 = sum2 + A[i];
          
    # Check if its a draw
    if (sum1 == sum2):
        print("-1");
      
    # If score of player 1 is greater
    elif(sum1 > sum2):
        print("Player 1");
      
    # Otherwise
    else:
        print("Player 2");
      
# Driver code
if __name__ == '__main__':
      
    str = "78787";
  
    determineWinner(str);
      
# This code is contributed by Amit Katiyar


C#
// C# program for the above approach 
using System;
class GFG{
  
// Function to find the winner of the
// game when both players play optimally
static void determineWinner(String str)
{
      
    // Stores the frequency of all digit
    int[] A = new int[10];
  
    // Stores the scores of player1
    // and player2 respectively
    int sum1 = 0, sum2 = 0;
  
    // Iterate to store frequencies
    for(int i = 0; i < str.Length; i++)
    {
        A[(int)str[i] - 48]++;
    }
  
    for(int i = 0; i <= 9; i++)
    {
          
        // Turn for the player1
        if (i % 2 != 0) 
        {
  
            // Add score of player1
            sum1 = sum1 + A[i];
        }
        else
        {
  
            // Add score of player2
            sum2 = sum2 + A[i];
        }
    }
  
    // Check if its a draw
    if (sum1 == sum2) 
    {
        Console.Write("-1");
    }
  
    // If score of player 1 is greater
    else if (sum1 > sum2)
    {
        Console.Write("Player 1");
    }
  
    // Otherwise
    else 
    {
        Console.Write("Player 2");
    }
}
  
// Driver code
public static void Main(String[] args)
{
    String str = "78787";
      
    determineWinner(str);
}
}
  
// This code is contributed by Rohit_ranjan


输出:
Player 1

时间复杂度: O(N)
辅助空间: O(N)