📌  相关文章
📜  通过删除非空子字符串来清空二进制字符串后找到最少为 0 的玩家

📅  最后修改于: 2021-09-24 05:03:23             🧑  作者: Mango

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

  • 玩家 1总是先开始。
  • 在每一轮中,玩家从给定的字符串删除一个非空的子字符串。
  • 在给定的字符串清空后,最小计数为0的玩家将赢得比赛。如果两个玩家的0秒数相等,则打印“ Tie ”。

例子:

方法:该问题可以基于以下观察来解决:

  • 如果字符串中0的计数是偶数,则玩家 1 和玩家 2 在每一轮中都选择子字符串“0” ,并且没有玩家会赢得这场比赛。
  • 否则,将连续1的计数存储在一个数组中,并在该数组上应用 nim 规则的游戏。
  • Nim-Sum:在游戏的任何时刻,每堆/堆中硬币/石头数量(此处为连续 1)的累积异或值称为该点的 Nim-Sum。

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

  • 初始化一个变量,比如cntZero ,以在字符串存储0的计数。
  • 初始化一个变量,比如cntConOne ,以存储字符串中连续1的计数。
  • 初始化一个变量,比如nimSum ,以存储给定字符串的连续1的 Nim-Sum 。
  • 遍历数组并计算0 s 和nimSum的计数。
  • 最后,检查cntZero的值是否为偶数。如果发现是真的,则打印Tie
  • 否则,检查nimSum的值是否大于0 。如果发现为真,则打印Player 1
  • 否则,打印player 2

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the player
// who wins the game
void FindwinnerOfGame(string& S)
{
 
    // Stores total count
    // of 0s in the string
    int cntZero = 0;
 
    // Stores count of
    // consecutive 1s
    int cntConOne = 0;
 
    // Stores Nim-Sum on count
    // of consecutive 1s
    int nimSum = 0;
 
    // Stores length
    // of the string
    int N = S.length();
 
    // Traverse the string
    for (int i = 0; i < N; i++) {
 
        // If the current
        // character is 1
        if (S[i] == '1') {
 
            // Update cntConOne
            cntConOne += 1;
        }
        else {
 
            // Update nimSum
            nimSum ^= cntConOne;
 
            // Update cntConOne
            cntConOne = 0;
 
            // Update cntZero
            cntZero++;
        }
    }
 
    // Update nimSum
    nimSum ^= cntConOne;
 
    // If countZero is
    // an even number
    if (cntZero % 2 == 0) {
        cout << "Tie";
    }
 
    // nimSum is not 0
    else if (nimSum) {
        cout << "player 1";
    }
 
    // If nimSum is zero
    else {
        cout << "player 2";
    }
}
 
// Driver Code
int main()
{
 
    string S = "0110011";
    FindwinnerOfGame(S);
}


Java
// Java program to implement
// the above approach
 
// Function to find the player
// who wins the game
class GFG {
    public static void FindwinnerOfGame(String S)
    {
 
        // Stores total count
        // of 0s in the string
        int cntZero = 0;
 
        // Stores count of
        // consecutive 1s
        int cntConOne = 0;
 
        // Stores Nim-Sum on count
        // of consecutive 1s
        int nimSum = 0;
 
        // Stores length
        // of the string
        int N = S.length();
 
        // Traverse the string
        for (int i = 0; i < N; i++) {
 
            // If the current
            // character is 1
            if (S.charAt(i) == '1') {
 
                // Update cntConOne
                cntConOne += 1;
            }
            else {
 
                // Update nimSum
                nimSum ^= cntConOne;
 
                // Update cntConOne
                cntConOne = 0;
 
                // Update cntZero
                cntZero++;
            }
        }
 
        // Update nimSum
        nimSum ^= cntConOne;
 
        // If countZero is
        // an even number
        if (cntZero % 2 == 0) {
            System.out.print("Tie");
        }
 
        // nimSum is not 0
        else if (nimSum != 0) {
            System.out.print("player 1");
        }
 
        // If nimSum is zero
        else {
            System.out.print("player 2");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "0110011";
        FindwinnerOfGame(S);
    }
}
 
// This code is contributed by grand_master.


Python3
# Python 3 program to implement
# the above approach
 
# Function to find the player
# who wins the game
def FindwinnerOfGame(S):
   
    # Stores total count
    # of 0s in the string
    cntZero = 0
 
    # Stores count of
    # consecutive 1s
    cntConOne = 0
 
    # Stores Nim-Sum on count
    # of consecutive 1s
    nimSum = 0
 
    # Stores length
    # of the string
    N = len(S)
 
    # Traverse the string
    for i in range(N):
       
        # If the current
        # character is 1
        if (S[i] == '1'):
           
            # Update cntConOne
            cntConOne += 1
        else:
           
            # Update nimSum
            nimSum ^= cntConOne
 
            # Update cntConOne
            cntConOne = 0
 
            # Update cntZero
            cntZero += 1
 
    # Update nimSum
    nimSum ^= cntConOne
 
    # If countZero is
    # an even number
    if (cntZero % 2 == 0):
        print("Tie")
 
    # nimSum is not 0
    elif(nimSum):
        print("player 1")
 
    # If nimSum is zero
    else:
        print("player 2")
 
# Driver Code
if __name__ == '__main__':
    S = "0110011"
    FindwinnerOfGame(S)
 
    # this code is contributed by SURENDRA_GANGWAR.


C#
// C# program to implement
// the above approach
using System;
 
// Function to find the player
// who wins the game
class GFG {
  public static void FindwinnerOfGame(string S)
  {
 
    // Stores total count
    // of 0s in the string
    int cntZero = 0;
 
    // Stores count of
    // consecutive 1s
    int cntConOne = 0;
 
    // Stores Nim-Sum on count
    // of consecutive 1s
    int nimSum = 0;
 
    // Stores length
    // of the string
    int N = S.Length;
 
    // Traverse the string
    for (int i = 0; i < N; i++) {
 
      // If the current
      // character is 1
      if (S[i] == '1') {
 
        // Update cntConOne
        cntConOne += 1;
      }
      else {
 
        // Update nimSum
        nimSum ^= cntConOne;
 
        // Update cntConOne
        cntConOne = 0;
 
        // Update cntZero
        cntZero++;
      }
    }
 
    // Update nimSum
    nimSum ^= cntConOne;
 
    // If countZero is
    // an even number
    if (cntZero % 2 == 0) {
      Console.Write("Tie");
    }
 
    // nimSum is not 0
    else if (nimSum != 0) {
      Console.Write("player 1");
    }
 
    // If nimSum is zero
    else {
      Console.Write("player 2");
    }
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    string S = "0110011";
    FindwinnerOfGame(S);
  }
}
 
// This code is contributed by ukasp.


Javascript


输出:
player 2

时间复杂度: O(N),其中 N 是字符串的长度
辅助空间: O(1)

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