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

📅  最后修改于: 2021-09-07 04:30:27             🧑  作者: Mango

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

  • 玩家 1 总是先开始。
  • 在一回合中,一名玩家可以从字符串移除连续的元素,移除的元素数量将累加到他的分数。玩家 1 将删除奇数的连续元素,玩家 1 将删除偶数的连续元素。
  • 任何不能移动的玩家都输掉了游戏。
  • 去除所有字符串后,得分最高的玩家获胜,如果得分相等,则打印“-1”

例子:

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

  1. 创建一个大小为 10 的数组A[]来存储给定字符串中所有数字的频率。
  2. 迭代给定的字符串并更新上述数组中每个数字的频率。
  3. 遍历数组A[]取两个变量sum1 = 0sum2 = 0来存储分数的总和。
  4. 如果指数为奇数,即玩家 1 回合,则增加sum1,如果指数为偶数,即玩家 2 回合,则增加sum2
  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


Javascript


输出:
Player 1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live