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

📅  最后修改于: 2021-09-05 08:50:16             🧑  作者: Mango

给定两个大小分别为NM的字符串arr[]brr[]数组,任务是在两个玩家按照以下规则进行最佳游戏时找到游戏的获胜者:

  • 玩家 1开始游戏。
  • 如果尚未从数组brr[] 中删除字符串,玩家 1将从数组arr[] 中删除该字符串。
  • 如果尚未从数组arr[] 中删除字符串,则播放器 2从数组brr[] 中删除该字符串。
  • 无法从数组中移除字符串的玩家,则该玩家将输掉游戏。

例子:

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

  • 如果来自两个数组的公共字符串的计数是奇数,则从数组brr[] 中删除一个字符串,因为玩家 1开始游戏并且玩家 1删除第一个公共字符串。
  • 如果在ARR字符串的计数[]是比在BRR []字符串的计数时通过去除来自两个阵列中的通用字符串,然后打印“玩家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


Javascript


输出:
Player 1

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

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