📌  相关文章
📜  找到最后一个从二进制字符串的开头删除任何字符的玩家

📅  最后修改于: 2021-09-03 13:46:32             🧑  作者: 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


Javascript


输出:
Player 2 wins

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

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