📜  给定一组对,找出其中的所有对称对

📅  最后修改于: 2021-10-27 09:00:13             🧑  作者: Mango

如果 c 等于 b 且 a 等于 d,则称两对 (a, b) 和 (c, d) 是对称的。例如,(10, 20) 和 (20, 10) 是对称的。给定一组对,找出其中的所有对称对。
可以假设所有对的第一个元素是不同的。
例子:

Input: arr[] = {{11, 20}, {30, 40}, {5, 10}, {40, 30}, {10, 5}}
Output: Following pairs have symmetric pairs
        (30, 40)
        (5, 10)  

我们强烈建议您将浏览器最小化,然后自己先尝试一下。
一个简单的解决方案是遍历每一对,并检查其他每一对是否对称。该解决方案需要 O(n 2 ) 时间。
更好的解决方案是使用排序。按第一个元素对所有对进行排序。对于每一对,对给定数组中的第二个元素进行二分搜索,即检查该对的第二个元素是否作为数组中的第一个元素存在。如果找到,则将 pair 的第一个元素与第二个元素进行比较。该解决方案的时间复杂度为 O(nLogn)。
一个有效的解决方案是使用哈希。 pair 的第一个元素用作键,第二个元素用作值。这个想法是一个一个地遍历所有对。对于每一对,检查它的第二个元素是否在哈希表中。如果是,则将第一个元素与哈希表的匹配条目的值进行比较。如果值和第一个元素匹配,那么我们找到了对称对。否则,插入第一个元素作为键,第二个元素作为值。
下面是这个想法的实现。

C++
#include
using namespace std;
 
// A C++ program to find all symmetric pairs in a given array of pairs
// Print all pairs that have a symmetric counterpart
void findSymPairs(int arr[][2], int row)
{
    // Creates an empty hashMap hM
    unordered_map hM;
 
    // Traverse through the given array
    for (int i = 0; i < row; i++)
    {
        // First and second elements of current pair
        int first = arr[i][0];
        int sec   = arr[i][1];
 
        // If found and value in hash matches with first
        // element of this pair, we found symmetry
        if (hM.find(sec) != hM.end() && hM[sec] == first)
            cout << "(" << sec << ", " << first << ")" <


Java
// A Java program to find all symmetric pairs in a given array of pairs
import java.util.HashMap;
  
class SymmetricPairs {
  
    // Print all pairs that have a symmetric counterpart
    static void findSymPairs(int arr[][])
    {
        // Creates an empty hashMap hM
        HashMap hM = new HashMap();
  
        // Traverse through the given array
        for (int i = 0; i < arr.length; i++)
        {
            // First and second elements of current pair
            int first = arr[i][0];
            int sec   = arr[i][1];
             
            // Look for second element of this pair in hash
            Integer val = hM.get(sec);
  
            // If found and value in hash matches with first
            // element of this pair, we found symmetry
            if (val != null && val == first)
               System.out.println("(" + sec + ", " + first + ")");
                
            else  // Else put sec element of this pair in hash
               hM.put(first, sec);
        }
    }
  
    // Driver method
    public static void main(String arg[])
    {
        int arr[][] = new int[5][2];
        arr[0][0] = 11; arr[0][1] = 20;
        arr[1][0] = 30; arr[1][1] = 40;
        arr[2][0] = 5;  arr[2][1] = 10;
        arr[3][0] = 40;  arr[3][1] = 30;
        arr[4][0] = 10;  arr[4][1] = 5;
        findSymPairs(arr);
    }
}


Python3
# A Python3 program to find all symmetric
# pairs in a given array of pairs.
 
# Print all pairs that have
# a symmetric counterpart
def findSymPairs(arr, row):
 
    # Creates an empty hashMap hM
    hM = dict()
 
    # Traverse through the given array
    for i in range(row):
         
        # First and second elements
        # of current pair
        first = arr[i][0]
        sec = arr[i][1]
 
        # If found and value in hash matches with first
        # element of this pair, we found symmetry
        if (sec in hM.keys() and hM[sec] == first):
            print("(", sec,",", first, ")")
 
        else: # Else put sec element of
              # this pair in hash
            hM[first] = sec
 
# Driver Code
if __name__ == '__main__':
    arr = [[0 for i in range(2)]
              for i in range(5)]
    arr[0][0], arr[0][1] = 11, 20
    arr[1][0], arr[1][1] = 30, 40
    arr[2][0], arr[2][1] = 5, 10
    arr[3][0], arr[3][1] = 40, 30
    arr[4][0], arr[4][1] = 10, 5
    findSymPairs(arr, 5)
 
# This code is contributed by Mohit Kumar


C#
// C# program to find all symmetric
// pairs in a given array of pairs
using System;
using System.Collections.Generic;
 
public class SymmetricPairs
{
 
    // Print all pairs that have a symmetric counterpart
    static void findSymPairs(int [,]arr)
    {
        // Creates an empty hashMap hM
        Dictionary hM = new Dictionary();
        int val = 0;
         
        // Traverse through the given array
        for (int i = 0; i < arr.GetLength(0); i++)
        {
            // First and second elements of current pair
            int first = arr[i, 0];
            int sec = arr[i, 1];
             
            // Look for second element of this pair in hash
            if(hM.ContainsKey(sec))
            val = hM[sec];
             
 
            // If found and value in hash matches with first
            // element of this pair, we found symmetry
            if (val != 0 && val == first)
            Console.WriteLine("(" + sec + ", " + first + ")");
                 
            else // Else put sec element of this pair in hash
            hM.Add(first, sec);
        }
    }
 
    // Driver code
    public static void Main(String []arg)
    {
        int [,]arr = new int[5, 2];
        arr[0, 0] = 11; arr[0, 1] = 20;
        arr[1, 0] = 30; arr[1, 1] = 40;
        arr[2, 0] = 5; arr[2, 1] = 10;
        arr[3, 0] = 40; arr[3, 1] = 30;
        arr[4, 0] = 10; arr[4, 1] = 5;
        findSymPairs(arr);
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:

Following pairs have symmetric pairs
(30, 40)
(5, 10)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程