📜  多集等价问题

📅  最后修改于: 2021-04-24 22:31:07             🧑  作者: Mango

与集合不同,多重集可能包含相同编号的多次出现。多集等价问题指出要检查两个给定的多集是否相等。例如,让A = {1,2,3}和B = {1,2,3}。在此设置了A ,但未设置B ( B中有1个出现两次),而AB都是多集。更正式,“是对定义为 \(A' = \{ (a, frequency(a)) | a \in \mathbf{A} \}\) 等于两个给定的多重集?”

给定两个多重集A和B,编写一个程序来检查两个多重集是否相等。

注意:多重集合中的元素的阶数为10 9

例子:

Input : A = {1, 1, 3, 4},  
        B = {1, 1, 3, 4}
Output : Yes

Input : A = {1, 3},  
        B = {1, 1}
Output : No

由于元素大至10 ^ 9,因此我们无法使用直接索引表。

一种解决方案是对两个多集进行排序并一一比较。

C++
// C++ program to check if two given multisets
// are equivalent
#include 
using namespace std;
  
bool areSame(vector& a, vector& b)
{
    // sort the elements of both multisets
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
  
    // Return true if both multisets are same.
    return (a == b);
}
  
int main()
{
    vector a({ 7, 7, 5 }), b({ 7, 5, 5 });
    if (areSame(a, b))
        cout << "Yes\n";
    else
        cout << "No\n";
    return 0;
}


Java
// Java program to check if two given multisets 
// are equivalent 
import java.util.*;
class GFG {
  
  
static boolean areSame(Vectora, Vectorb) 
{ 
    // sort the elements of both multisets 
    Collections.sort(a); 
    Collections.sort(b); 
  
    // Return true if both multisets are same. 
    return (a == b); 
} 
 public static void main(String[] args) {
       Vector a = new Vector(Arrays.asList( 7, 7, 5 ));
       Vector b = new Vector(Arrays.asList( 7, 5, 5));
    if (areSame(a, b)) 
        System.out.print("Yes\n"); 
    else
        System.out.print("No\n");
    }
}
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to check if 
# two given multisets are equivalent
  
def areSame(a, b):
      
    # sort the elements of both multisets
    a.sort();
    b.sort();
  
    # Return true if both multisets are same.
    return (a == b);
  
# Driver Code
a = [ 7, 7, 5 ];
b = [ 7, 5, 5 ];
if (areSame(a, b)):
    print("Yes");
else:
    print("No");
  
# This code is contributed by Princi Singh


C#
// C# program to check if two given multisets 
// are equivalent 
using System;
using System.Collections.Generic;
  
class GFG
{
  
static bool areSame(Lista, Listb) 
{ 
    // sort the elements of both multisets 
    a.Sort();
    b.Sort();
  
    // Return true if both multisets are same. 
    return (a == b); 
} 
  
// Driver code
public static void Main()
{
    List a = new List { 7, 7, 5 };
    List b = new List { 7, 5, 5 };
    if (areSame(a, b)) 
        Console.WriteLine("Yes\n"); 
    else
        Console.WriteLine("No\n");
}
}
  
// This code is contributed by Rajput-Ji


C++
// C++ program to check if two given multisets
// are equivalent
#include 
using namespace std;
  
bool areSame(vector& a, vector& b)
{
    if (a.size() != b.size())
        return false;
  
    // Create two unordered maps m1 and m2
    // and insert values of both vectors.
    unordered_map m1, m2;
    for (int i = 0; i < a.size(); i++) {
        m1[a[i]]++;
        m2[b[i]]++;
    }
  
    // Now we check if both unordered_maps
    // are same of not.
    for (auto x : m1) {
        if (m2.find(x.first) == m2.end() || 
            m2[x.first] != x.second)
            return false;
    }
  
    return true;
}
  
// Driver code
int main()
{
    vector a({ 7, 7, 5 }), b({ 7, 7, 5 });
    if (areSame(a, b))
        cout << "Yes\n";
    else
        cout << "No\n";
    return 0;
}


Java
// Java program to check if two given multisets
// are equivalent
import java.util.*;
  
class GFG 
{
static boolean areSame(int []a, int []b)
{
    if (a.length != b.length)
        return false;
  
    // Create two unordered maps m1 and m2
    // and insert values of both vectors.
    HashMap m1, m2;
    m1 = new HashMap();
    m2 = new HashMap();
    for (int i = 0; i < a.length; i++)
    {
        if(m1.containsKey(a[i]))
        {
            m1.put(a[i], m1.get(a[i]) + 1);
        }
        else
        {
            m1.put(a[i], 1);
        }
        if(m2.containsKey(b[i]))
        {
            m2.put(b[i], m2.get(b[i]) + 1);
        }
        else
        {
            m2.put(b[i], 1);
        }
    }
  
    // Now we check if both unordered_maps
    // are same of not.
    for (Map.Entry x : m1.entrySet())
    {
        if (!m2.containsKey(x.getKey()) || 
             m2.get(x.getKey()) != x.getValue())
            return false;
    }
    return true;
}
  
// Driver code
public static void main(String args[]) 
{
    int []a = { 7, 7, 5 };
    int []b = { 7, 7, 5 };
    if (areSame(a, b))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by 29AjayKumar


C#
// C# program to check if two given multisets
// are equivalent 
using System;
using System.Collections.Generic;
  
class GFG 
{
static bool areSame(int []a, int []b)
{
    if (a.Length != b.Length)
        return false;
  
    // Create two unordered maps m1 and m2
    // and insert values of both vectors.
    Dictionary m1, m2;
    m1 = new Dictionary();
    m2 = new Dictionary();
    for (int i = 0; i < a.Length; i++)
    {
        if(m1.ContainsKey(a[i]))
        {
            m1[a[i]] = m1[a[i]] + 1;
        }
        else
        {
            m1.Add(a[i], 1);
        }
        if(m2.ContainsKey(b[i]))
        {
            m2[b[i]] = m2[b[i]] + 1;
        }
        else
        {
            m2.Add(b[i], 1);
        }
    }
  
    // Now we check if both unordered_maps
    // are same of not.
    foreach(KeyValuePair x in m1)
    {
        if (!m2.ContainsKey(x.Key) || 
             m2[x.Key] != x.Value)
            return false;
    }
    return true;
}
  
// Driver code
public static void Main(String []args) 
{
    int []a = { 7, 7, 5 };
    int []b = { 7, 7, 5 };
    if (areSame(a, b))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by 29AjayKumar


输出:
No

更好的解决方案是使用哈希。我们创建两个空的哈希表(在C++中使用unordered_map实现)。我们首先在第一个表中插入第一个多图的所有项目,然后在第二个表中插入第二个多集的所有项目。现在,我们检查两个哈希表是否包含相同的项目和频率。

C++

// C++ program to check if two given multisets
// are equivalent
#include 
using namespace std;
  
bool areSame(vector& a, vector& b)
{
    if (a.size() != b.size())
        return false;
  
    // Create two unordered maps m1 and m2
    // and insert values of both vectors.
    unordered_map m1, m2;
    for (int i = 0; i < a.size(); i++) {
        m1[a[i]]++;
        m2[b[i]]++;
    }
  
    // Now we check if both unordered_maps
    // are same of not.
    for (auto x : m1) {
        if (m2.find(x.first) == m2.end() || 
            m2[x.first] != x.second)
            return false;
    }
  
    return true;
}
  
// Driver code
int main()
{
    vector a({ 7, 7, 5 }), b({ 7, 7, 5 });
    if (areSame(a, b))
        cout << "Yes\n";
    else
        cout << "No\n";
    return 0;
}

Java

// Java program to check if two given multisets
// are equivalent
import java.util.*;
  
class GFG 
{
static boolean areSame(int []a, int []b)
{
    if (a.length != b.length)
        return false;
  
    // Create two unordered maps m1 and m2
    // and insert values of both vectors.
    HashMap m1, m2;
    m1 = new HashMap();
    m2 = new HashMap();
    for (int i = 0; i < a.length; i++)
    {
        if(m1.containsKey(a[i]))
        {
            m1.put(a[i], m1.get(a[i]) + 1);
        }
        else
        {
            m1.put(a[i], 1);
        }
        if(m2.containsKey(b[i]))
        {
            m2.put(b[i], m2.get(b[i]) + 1);
        }
        else
        {
            m2.put(b[i], 1);
        }
    }
  
    // Now we check if both unordered_maps
    // are same of not.
    for (Map.Entry x : m1.entrySet())
    {
        if (!m2.containsKey(x.getKey()) || 
             m2.get(x.getKey()) != x.getValue())
            return false;
    }
    return true;
}
  
// Driver code
public static void main(String args[]) 
{
    int []a = { 7, 7, 5 };
    int []b = { 7, 7, 5 };
    if (areSame(a, b))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by 29AjayKumar

C#

// C# program to check if two given multisets
// are equivalent 
using System;
using System.Collections.Generic;
  
class GFG 
{
static bool areSame(int []a, int []b)
{
    if (a.Length != b.Length)
        return false;
  
    // Create two unordered maps m1 and m2
    // and insert values of both vectors.
    Dictionary m1, m2;
    m1 = new Dictionary();
    m2 = new Dictionary();
    for (int i = 0; i < a.Length; i++)
    {
        if(m1.ContainsKey(a[i]))
        {
            m1[a[i]] = m1[a[i]] + 1;
        }
        else
        {
            m1.Add(a[i], 1);
        }
        if(m2.ContainsKey(b[i]))
        {
            m2[b[i]] = m2[b[i]] + 1;
        }
        else
        {
            m2.Add(b[i], 1);
        }
    }
  
    // Now we check if both unordered_maps
    // are same of not.
    foreach(KeyValuePair x in m1)
    {
        if (!m2.ContainsKey(x.Key) || 
             m2[x.Key] != x.Value)
            return false;
    }
    return true;
}
  
// Driver code
public static void Main(String []args) 
{
    int []a = { 7, 7, 5 };
    int []b = { 7, 7, 5 };
    if (areSame(a, b))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by 29AjayKumar
输出:
Yes

时间复杂度:假设unordered_map find()和insert()操作在O(1)时间内工作,则O(n)。