给定二进制字符串S。该任务是确定游戏的赢家当两个玩家玩游戏最佳用字符串按给定的条件:
- 玩家1总是最先开始。
- 两名玩家轮流选择一个完整的连续等号字符块,并将其从给定的二进制String S中删除。
- 播放器1只能选择奇数个连续的相等字符,而播放器2只能选择偶数个连续的相等字符。只有在不可能选择任何东西的情况下,玩家才可以选择任何东西并将其计为回合。
- 删除所有字符后,得分最高的玩家将赢得比赛,如果得分相等,则显示“ -1”
Input: S = “1110011001010”
Output: Player 1
Explanation:
The selected characters will be in bold and Player 1’s score is Score_1 and Player 2’s score is Score_2 :
Turn 1 : (Player 1) “1110011001010” → “0011001010” Score_1 = 3
Turn 2 : (Player 2) “0011001010” → “00001010” Score_2 = 2
Turn 3 : (Player 1) “00001010”→ “0000010” Score_1 = 4
Turn 4: (Player 2) “0000010”
He cannot do anything as only one ‘1’ is present which is an odd number.
Also, he can’t choose the ‘0’s as they are odd (5 and 1), Therefore, Score_2 =2
Turn 5:(Player 1) “0000010”→ “000000” Score_1=5
Turn 6:(Player 2) “000000” → “” Score_2 = 2 (No ‘1’ was deleted in this turn)
Final scores: Score_1 = 5 and Score_2 = 2
Therefore, Player 1 wins.
Input : S = “11111101”
Output: Player 2
Explanation:
Turn 1 : (Player 1) “11111101” → “1111110” Score_1 = 3
Turn 2 : (Player 2) “1111110” → “0” Score_2 = 6
Turn 3 : (Player 1) “0” → “” Score_1 = 3
Final scores: Score_1 = 3 and Score_2 = 6
Therefore, Player 2 wins.
方法:
- 如果我们仔细观察这个游戏,我们就会知道,只有连续的1有助于这些玩家的得分。
- 创建一个列表来存储字符串连续1的长度。
- 按降序对列表进行排序。
- 现在遍历列表,如果list元素为奇数,它将被添加到播放器1的分数中,如果是偶数,则将其添加到播放器2的分数中。
- 现在,如果玩家1的分数大于玩家2的分数,则打印“玩家1”,并且如果玩家2的分数大于玩家1的分数,则打印“玩家2” 。
- 如果打成平局,则打印“ -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
输出:
Player 2
时间复杂度: O(N)
辅助空间: O(N)