📌  相关文章
📜  找到最后一位从二进制字符串开头删除任何字符的播放器

📅  最后修改于: 2021-04-22 09:59:05             🧑  作者: Mango

给定一个由二进制字符串组成的数组arr [] ,任务是当两个玩家按照以下规则最佳玩游戏时,找到游戏的获胜者:

  • 玩家1开始游戏。
  • 在每一回合中,玩家必须选择一个非空字符串,并从字符串开头删除正数个字符。
  • 播放器1只能选择以字符“ 0”开头的字符串,而播放器2只能选择以字符“ 1”开头的字符串。
  • 不能采取行动的玩家将输掉比赛。

例子:

方法:想法是比较如果两个玩家都玩得最好的话,每个玩家可以做出的动作总数。请按照以下步骤操作:

  1. 如果在任何字符串中连续出现同一字符,则只需用该字符的一次出现替换它们,因为最好删除开头出现的所有字符。
  2. 现在,如果字符串的起始元素与最后一个元素相同,那么即使没有此字符串,游戏的场景也将保持不变,因为如果一个玩家在此字符串上移动,则另一位玩家通过删除该字符进行下一移动来自相同的字符串,导致第一个玩家的位置完全相同。
  3. 如果字符串的起始元素与最后一个元素不同,则它要求演奏者多做一些动作。
  4. 因此,只需计算每个玩家必须执行的额外动作数即可。
  5. 用完多余动作的玩家将输掉比赛。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find the player who
// loses the game
void findPlayer(string str[], int n)
{
  
    // Moves for the first player
    int move_first = 0;
  
    // Moves for the second player
    int move_sec = 0;
  
    // Iterate over array of strings
    for (int i = 0; i < n; i++) {
  
        // Check if the first and last
        // character are the same
        if (str[i][0]
            == str[i][str[i].length() - 1]) {
  
            // Check if string start and
            // end with character '0'
            if (str[i][0] == 48)
                move_first++;
            else
                move_sec++;
        }
    }
  
    // If first player have less moves
    if (move_first <= move_sec) {
        cout << "Player 2 wins";
    }
    else {
        cout << "Player 1 wins";
    }
}
  
// Driver Code
int main()
{
    // Given array of strings
    string str[] = { "010", "101" };
  
    int N = sizeof(str)
            / sizeof(str[0]);
  
    // Function Call
    findPlayer(str, N);
  
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
  
// Function to find the player who
// loses the game
static void findPlayer(String str[],
                       int n)
{
  // Moves for the
  // first player
  int move_first = 0;
 
  // Moves for the
  // second player
  int move_sec = 0;
 
  // Iterate over array
  // of Strings
  for (int i = 0; i < n - 1; i++)
  {
    // Check if the first and last
    // character are the same
    if (str[i].charAt(0) ==
        str[i].charAt(str[i].length() - 1))
    {
      // Check if String start and
      // end with character '0'
      if (str[i].charAt(0) == 48)
        move_first++;
      else
        move_sec++;
    }
  }
 
  // If first player have less moves
  if (move_first <= move_sec)
  {
    System.out.print("Player 2 wins");
  }
  else
  {
    System.out.print("Player 1 wins");
  }
}
  
// Driver Code
public static void main(String[] args)
{
  // Given array of Strings
  String str[] = {"010", "101"};
 
  int N = str[0].length();
   
  // Function Call
  findPlayer(str, N);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
  
# Function to find the player who
# loses the game
def findPlayer(str, n):
  
    # Moves for the first player
    move_first = 0
  
    # Moves for the second player
    move_sec = 0
  
    # Iterate over array of strings
    for i in range(n):
  
        # Check if the first and last
        # character are the same
        if (str[i][0] ==
            str[i][len(str[i]) - 1]):
  
            # Check if string start and
            # end with character '0'
            if (str[i][0] == 48):
                move_first += 1
            else:
                move_sec += 1
         
    # If first player have less moves
    if (move_first <= move_sec):
        print("Player 2 wins")
    else:
        print("Player 1 wins")
     
# Driver Code
 
# Given array of strings
str = [ "010", "101" ]
  
N = len(str)
  
# Function call
findPlayer(str, N)
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach 
using System;
 
class GFG{
  
// Function to find the player who
// loses the game
static void findPlayer(string[] str, int n)
{
     
    // Moves for the first player
    int move_first = 0;
  
    // Moves for the second player
    int move_sec = 0;
  
    // Iterate over array of strings
    for(int i = 0; i < n; i++)
    {
         
        // Check if the first and last
        // character are the same
        if (str[i][0] ==
            str[i][str[i].Length - 1])
        {
             
            // Check if string start and
            // end with character '0'
            if ((str[i][0]) == 48)
                move_first++;
            else
                move_sec++;
        }
    }
  
    // If first player have less moves
    if (move_first <= move_sec)
    {
        Console.Write("Player 2 wins");
    }
    else
    {
        Console.Write("Player 1 wins");
    }
}
  
// Driver Code
public static void Main ()
{
     
    // Given array of strings
    string[] str = { "010", "101" };
  
    int N = str.Length;
  
    // Function call
    findPlayer(str, N);
}
}
 
// This code is contributed by sanjoy_62


输出:
Player 2 wins




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