📜  二元字符串游戏

📅  最后修改于: 2021-09-07 03:00:58             🧑  作者: Mango

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

  • 玩家 1 总是先开始。
  • 两个玩家轮流选择一整块连续的相等字符并将它们从给定的二进制字符串S 中删除
  • 玩家 1 只能选择奇数个连续相等的字符,玩家 2 只能选择偶数个连续相等的字符。只有在无法选择任何东西时,玩家才可以不选择任何东西并将其视为自己的回合。
  • 删除所有字符后,得分最高的玩家获胜,如果得分相等则打印“-1”

方法:

  1. 如果我们仔细观察这场比赛,我们就会明白,只有连续的1对这些球员的得分有所贡献。
  2. 创建一个列表来存储字符串连续 1 的长度。
  3. 按降序对列表进行排序。
  4. 现在迭代列表,如果列表元素是奇数,它将被添加到玩家 1的分数中,如果是偶数,它将被添加到玩家 2的分数中。
  5. 现在,如果玩家 1 的分数大于玩家 2 的分数,则打印“玩家 1” ,如果玩家 2 的分数大于玩家 1 的分数,则打印“玩家 2”
  6. 如果有平局,即分数相同,则打印“-1”。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to return the result of
// the game
string gameMax(string S)
{
     
    // length of the string
    int N = S.length();
  
    // List to maintain the lengths of
    // consecutive '1's in the string
    vector list;
  
    // Variable that keeps a track of
    // the current length of the block
    // of consecutive '1's
    int one = 0;
  
    for(int i = 0; i < N; i++)
    {
        if (S[i] == '1')
        {
            one++;
        }
        else
        {
             
            // Adds non-zero lengths
            if (one != 0)
            {
                list.push_back(one);
            }
            one = 0;
        }
    }
  
    // This takes care of the case
    // when the last character is '1'
    if (one != 0)
    {
        list.push_back(one);
    }
  
    // Sorts the lengths in
    // descending order
    sort(list.begin(), list.end(),
         greater());
  
    // Scores of the 2 players
    int score_1 = 0, score_2 = 0;
  
    for(int i = 0; i < list.size(); i++)
    {
         
        // For player 1
        if (list[i] % 2 == 1)
        {
            score_1 += list[i];
        }
  
        // For player 2
        else
        {
            score_2 += list[i];
        }
    }
  
    // In case of a tie
    if (score_1 == score_2)
        return "-1";
  
    // Print the result
    return (score_1 > score_2) ? "Player 1" :
                                 "Player 2";
}
 
// Driver Code
int main()
{
     
    // Given string S
    string S = "11111101";
  
    // Function call
    cout << gameMax(S);
}
 
// This code is contributed by rutvik_56


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to return the result of
    // the game
    public static String gameMax(String S)
    {
        // length of the string
        int N = S.length();
 
        // List to maintain the lengths of
        // consecutive '1's in the string
        List list = new ArrayList<>();
 
        // Variable that keeps a track of
        // the current length of the block
        // of consecutive '1's
        int one = 0;
 
        for (int i = 0; i < N; i++) {
 
            if (S.charAt(i) == '1') {
                one++;
            }
            else {
 
                // Adds non-zero lengths
                if (one != 0) {
                    list.add(one);
                }
                one = 0;
            }
        }
 
        // This takes care of the case
        // when the last character is '1'
        if (one != 0) {
            list.add(one);
        }
 
        // Sorts the lengths in
        // descending order
        Collections.sort(list,
                         Collections.reverseOrder());
 
        // Scores of the 2 players
        int score_1 = 0, score_2 = 0;
 
        for (int i = 0; i < list.size(); i++) {
 
            // For player 1
            if (list.get(i) % 2 == 1) {
                score_1 += list.get(i);
            }
 
            // For player 2
            else {
                score_2 += list.get(i);
            }
        }
 
        // In case of a tie
        if (score_1 == score_2)
            return "-1";
 
        // Print the result
        return (score_1 > score_2) ? "Player 1"
                                   : "Player 2";
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given string S
        String S = "11111101";
 
        // Function Call
        System.out.println(gameMax(S));
    }
}


Python3
# Python3 program for
# the above approach
 
# Function to return the
# result of the game
def gameMax(S):
 
    # Length of the string
    N = len(S)
 
    # List to maintain the lengths of
    # consecutive '1's in the string
    list = []
 
    # Variable that keeps a track of
    # the current length of the block
    # of consecutive '1's
    one = 0
     
    for i in range(N):
        if(S[i] == '1'):
            one += 1
        else:
 
            # Adds non-zero lengths
            if(one != 0):
                list.append(one)
            one = 0
 
    # This takes care of the case
    # when the last character is '1'
    if(one != 0):
        list.append(one)
 
    # Sorts the lengths in
    # descending order
    list.sort(reverse = True)
 
    # Scores of the 2 players
    score_1 = 0
    score_2 = 0
 
    for i in range(len(list)):
 
        # For player 1
        if(list[i] % 2 == 1):
            score_1 += list[i]
 
        # For player 2
        else:
            score_2 += list[i]
 
    # In case of a tie
    if(score_1 == score_2):
        return '-1'
 
    # Print the result
    if(score_1 > score_2):
        return "Player 1"
    else:
        return "Player 2"
 
# Driver Code
 
# Given string S
S = "11111101"
 
# Function call
print(gameMax(S))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to return the result of
// the game
public static String gameMax(String S)
{
     
    // length of the string
    int N = S.Length;
 
    // List to maintain the lengths of
    // consecutive '1's in the string
    List list = new List();
 
    // Variable that keeps a track of
    // the current length of the block
    // of consecutive '1's
    int one = 0;
 
    for(int i = 0; i < N; i++)
    {
        if (S[i] == '1')
        {
            one++;
        }
        else
        {
 
            // Adds non-zero lengths
            if (one != 0)
            {
                list.Add(one);
            }
            one = 0;
        }
    }
 
    // This takes care of the case
    // when the last character is '1'
    if (one != 0)
    {
        list.Add(one);
    }
 
    // Sorts the lengths in
    // descending order
    list.Sort();
    list.Reverse();
     
    // Scores of the 2 players
    int score_1 = 0, score_2 = 0;
 
    for(int i = 0; i < list.Count; i++)
    {
         
        // For player 1
        if (list[i] % 2 == 1)
        {
            score_1 += list[i];
        }
 
        // For player 2
        else
        {
            score_2 += list[i];
        }
    }
 
    // In case of a tie
    if (score_1 == score_2)
        return "-1";
 
    // Print the result
    return (score_1 > score_2) ?
         "Player 1" : "Player 2";
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given string S
    String S = "11111101";
 
    // Function Call
    Console.WriteLine(gameMax(S));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:

Player 2

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

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