给定一个二进制字符串S ,任务是确定当两个玩家按照给定的字符串按照以下条件交替轮流最佳玩游戏时,确定游戏的获胜者:
- 玩家1总是最先开始。
- 在每个回合中,玩家都会从给定的字符串删除一个非空的子字符串。
- 在清空给定的字符串后,最小计数为0 s的玩家将赢得比赛。如果两个玩家的计数相等为0 s,则打印“ Tie ”。
例子:
Input: S = “00011”
Output: Player 1
Explanation: Substrings can be chosen as follows:
Turn 1: Player 1 removes the substring S[4…5]. Therefore, Player 1 contains “11”.
Turn 2: Player 2 removes the substring S[0…0]. Therefore, Player 2 contains “0”.
Turn 3: Player 1 removes the substring S[0…0]. Therefore, Player 1 contains “110”.
Turn 4: Player 2 removes the substring S[0…0]. Therefore, Player 2 contains “00”.
Therefore, Player 1 wins the game.
Input: S = “0110011”
Output: Player 2
方法:可以根据以下观察结果解决问题:
- 如果字符串的0 s计数为偶数,则玩家1和玩家2每次都会选择子字符串“ 0” ,并且没有玩家会赢得这场比赛。
- 否则,将连续1 s的计数存储在数组中,然后将nim规则的游戏应用于数组。
- Nim-Sum:在游戏的任何一点上,每堆/堆中的硬币/石头的数量(此处为连续1s)的累积XOR值在该点称为Nim-Sum。
请按照以下步骤解决问题:
- 初始化一个变量,例如cntZero ,将0的计数存储在字符串。
- 初始化一个变量cntConOne ,将连续1 s的计数存储在字符串。
- 初始化一个变量,例如nimSum ,以存储给定字符串的连续1 s的Nim-Sum。
- 遍历数组并计算0 s和nimSum的计数。
- 最后,检查cntZero的值是否为偶数。如果发现是真的,则打印Tie 。
- 否则,请检查nimSum的值是否大于0 。如果发现是真的,则打印Player 1 。
- 否则,请打印播放器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.
输出:
player 2
时间复杂度: O(N),其中N是字符串的长度
辅助空间: O(1)