📌  相关文章
📜  找到最后一个能够从一个数组中删除一个尚未从其他数组中删除的字符串的播放器

📅  最后修改于: 2021-05-19 18:57:22             🧑  作者: Mango

给定分别为NM的两个字符串数组arr []brr [] ,任务是当两个玩家按照以下规则最佳玩游戏时,找到游戏的赢家:

  • 玩家1开始游戏。
  • 播放器1删除该数组ARR一个字符串,如果[]它尚未从阵列BRR []删除。
  • 播放机2删除该数组BRR一个字符串,如果[]它尚未从阵列ARR []删除。
  • 无法从数组中删除字符串的玩家将失去游戏。

例子:

方法:基于以下事实的想法两个数组中的公共字符串只能从一个数组中删除。请按照以下步骤解决问题:

  • 如果来自两个数组的公共字符串的计数均为奇数,则从玩家brr []中删除一个字符串,因为玩家1开始游戏,并且玩家1删除了第一个公共字符串。
  • 如果通过从两个数组中删除公共字符串, arr []中的字符串数大于brr []中的字符串,则打印“ Player 1”
  • 否则,打印“ Player 2”

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include
using namespace std;
  
// Function to find last player to be
// able to remove a string from one array
// which has not been removed from the other array
void lastPlayer(int n, int m, vector arr,
                       vector brr)
{
 
    // Stores common strings
    // from both the array
   set common;
 
    for (int i = 0; i < arr.size(); i++)
    {
        for (int j = 0; j < brr.size(); j++)
        {
            if (arr[i] == brr[j])
            {
 
                // add common elements
                common.insert(arr[i]);
                break;
            }
        }
    }
 
    // Removing common strings from arr[]
    set a;
    bool flag;
    for (int i = 0; i < arr.size(); i++)
    {
        flag = false;
        for (auto value : common)
        {
            if (value == arr[i])
            {
 
                // add common elements
                flag = true;
                break;
            }
        }
        if (flag)
            a.insert(arr[i]);
    }
 
    // Removing common elements from B
    set b;
    for (int i = 0; i < brr.size(); i++)
    {
        flag = false;
        for (auto value : common)
        {
            if (value == brr[i])
            {
 
                // add common elements
                flag = true;
                break;
            }
        }
 
        if (flag)
            b.insert(brr[i]);
    }
 
    // Stores strings in brr[] which
    // is not common in arr[]
    int LenBrr = b.size();
    if ((common.size()) % 2 == 1)
    {
 
        // Update LenBrr
        LenBrr -= 1;
    }
 
    if (a.size() > LenBrr)
    {
        cout<<("Player 1")< arr{ "geeks", "geek" };
 
    // Set of strings for player B
    vector brr{ "geeks", "geeksforgeeks" };
    int n = arr.size();
    int m = brr.size();
    lastPlayer(n, m, arr, brr);
}
 
// This code is contributed by SURENDRA_GANGWAR.


Java
// Java Program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
    // Function to find last player to be
    // able to remove a string from one array
    // which has not been removed from the other array
    static void lastPlayer(int n, int m, String[] arr,
                           String[] brr)
    {
 
        // Stores common strings
        // from both the array
        Set common = new HashSet<>();
 
        for (int i = 0; i < arr.length; i++)
        {
            for (int j = 0; j < brr.length; j++)
            {
                if (arr[i] == brr[j])
                {
 
                    // add common elements
                    common.add(arr[i]);
                    break;
                }
            }
        }
 
        // Removing common strings from arr[]
        Set a = new HashSet<>();
        boolean flag;
        for (int i = 0; i < arr.length; i++)
        {
            flag = false;
            for (String value : common)
            {
                if (value == arr[i])
                {
 
                    // add common elements
                    flag = true;
                    break;
                }
            }
            if (flag)
                a.add(arr[i]);
        }
 
        // Removing common elements from B
        Set b = new HashSet<>();
        for (int i = 0; i < brr.length; i++)
        {
            flag = false;
            for (String value : common)
            {
                if (value == brr[i])
                {
 
                    // add common elements
                    flag = true;
                    break;
                }
            }
 
            if (flag)
                b.add(brr[i]);
        }
 
        // Stores strings in brr[] which
        // is not common in arr[]
        int LenBrr = b.size();
        if ((common.size()) % 2 == 1)
        {
 
            // Update LenBrr
            LenBrr -= 1;
        }
 
        if (a.size() > LenBrr)
        {
            System.out.print("Player 1");
        }
        else
        {
            System.out.print("Player 2");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Set of strings for player A
        String[] arr = { "geeks", "geek" };
 
        // Set of strings for player B
        String[] brr = { "geeks", "geeksforgeeks" };
        int n = arr.length;
        int m = brr.length;
        lastPlayer(n, m, arr, brr);
    }
}
 
// This code is contributed by Dharanendra L V.


Python
# Python Program for the above approach
 
 
# Function to find last player to be
# able to remove a string from one array
# which has not been removed from the other array
def lastPlayer(n, m, arr, brr):
 
    # Stores common strings
    # from both the array
    common = list(set(arr) & set(brr))
 
    # Removing common strings from arr[]
    a = list(set(arr) ^ set(common))
 
    # Removing common elements from B
    b = list(set(brr) ^ set(common))
 
    # Stores strings in brr[] which
    # is not common in arr[]
    LenBrr = len(b)
 
    if len(common) % 2 == 1:
 
        # Update LenBrr
        LenBrr -= 1
     
    if len(a) > LenBrr:
        print("Player 1")
    else:
        print("Player 2")
 
 
# Driver Code
if __name__ == '__main__':
 
    # Set of strings for player A
    arr = ["geeks", "geek"]
 
    # Set of strings for player B
    brr = ["geeks", "geeksforgeeks"]
     
    n = len(arr)
    m = len(brr)
 
    lastPlayer(n, m, arr, brr)


C#
// C# Program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
   
    // Function to find last player to be
    // able to remove a string from one array
    // which has not been removed from the other array
    static void lastPlayer(int n, int m, String[] arr,
                           String[] brr)
    {
 
        // Stores common strings
        // from both the array
        HashSet common = new HashSet();
        for (int i = 0; i < arr.Length; i++)
        {
            for (int j = 0; j < brr.Length; j++)
            {
                if (arr[i] == brr[j])
                {
 
                    // add common elements
                    common.Add(arr[i]);
                    break;
                }
            }
        }
 
        // Removing common strings from []arr
        HashSet a = new HashSet();
        bool flag;
        for (int i = 0; i < arr.Length; i++)
        {
            flag = false;
            foreach (String value in common)
            {
                if (value == arr[i])
                {
 
                    // add common elements
                    flag = true;
                    break;
                }
            }
            if (flag)
                a.Add(arr[i]);
        }
 
        // Removing common elements from B
        HashSet b = new HashSet();
        for (int i = 0; i < brr.Length; i++)
        {
            flag = false;
            foreach (String value in common)
            {
                if (value == brr[i])
                {
 
                    // add common elements
                    flag = true;
                    break;
                }
            }
 
            if (flag)
                b.Add(brr[i]);
        }
 
        // Stores strings in brr[] which
        // is not common in []arr
        int LenBrr = b.Count;
        if ((common.Count) % 2 == 1)
        {
 
            // Update LenBrr
            LenBrr -= 1;
        }
 
        if (a.Count > LenBrr)
        {
            Console.Write("Player 1");
        }
        else
        {
            Console.Write("Player 2");
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
       
        // Set of strings for player A
        String[] arr = { "geeks", "geek" };
 
        // Set of strings for player B
        String[] brr = { "geeks", "geeksforgeeks" };
        int n = arr.Length;
        int m = brr.Length;
        lastPlayer(n, m, arr, brr);
    }
}
 
// This code is contributed by 29AjayKumar


输出:
Player 1

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