📜  螺母和螺栓问题(锁和钥匙问题)|设置 2(哈希图)

📅  最后修改于: 2021-10-27 20:28:11             🧑  作者: Mango

给定一组 n 个不同尺寸的螺母和 n 个不同尺寸的螺栓。螺母和螺栓之间存在一对一的映射关系。有效匹配螺母和螺栓。
约束:不允许将螺母与另一个螺母或螺栓与另一个螺栓进行比较。这意味着螺母只能与螺栓相比,螺栓只能与螺母相比,看哪个更大/更小。
例子:

Input : nuts[] = {'@', '#', '$', '%', '^', '&'}
        bolts[] = {'$', '%', '&', '^', '@', '#'}
Output : Matched nuts and bolts are-
        $ % & ^ @ # 
        $ % & ^ @ #  

提出这个问题的另一种方法是,给定一个带锁和钥匙的盒子,其中一把锁可以用盒子中的一把钥匙打开。我们需要匹配这对。

我们在下面的帖子中讨论了基于排序的解决方案。
螺母和螺栓问题(锁和钥匙问题)|设置 1
在这篇文章中,讨论了基于哈希图的方法。

  1. 遍历nuts数组并创建一个hashmap
  2. 遍历 bolts 数组并在 hashmap 中搜索它。
  3. 如果它在螺母的哈希图中找到,那么这意味着该螺母存在螺栓。
C++
// Hashmap based solution to solve
#include 
using namespace std;
 
// function to match nuts and bolts
void nutboltmatch(char nuts[], char bolts[], int n)
{
    unordered_map hash;
 
    // creating a hashmap for nuts
    for (int i = 0; i < n; i++)
        hash[nuts[i]] = i;
 
    // searching for nuts for each bolt in hash map
    for (int i = 0; i < n; i++)
        if (hash.find(bolts[i]) != hash.end())
            nuts[i] = bolts[i];
 
    // print the result
    cout << "matched nuts and bolts are-" << endl;
    for (int i = 0; i < n; i++)
        cout << nuts[i] << " ";
    cout << endl;
    for (int i = 0; i < n; i++)
        cout << bolts[i] << " ";
}
 
// Driver code
int main()
{
    char nuts[] = {'@', '#', '$', '%', '^', '&'};
    char bolts[] = {'$', '%', '&', '^', '@', '#'};
    int n = sizeof(nuts) / sizeof(nuts[0]);
    nutboltmatch(nuts, bolts, n);
    return 0;
}


Java
// Hashmap based solution to solve
import java.util.HashMap;
class GFG
{
 
  // function to match nuts and bolts
  static void nutboltmatch(char nuts[], char bolts[], int n)
  {
    HashMap hash = new HashMap<>();
 
    // creating a hashmap for nuts
    for (int i = 0; i < n; i++)
      hash.put(nuts[i], i);
 
    // searching for nuts for each bolt in hash map
    for (int i = 0; i < n; i++)
      if (hash.containsKey(bolts[i]))
        nuts[i] = bolts[i];
 
    // print the result
    System.out.println("matched nuts and bolts are-");
    for (int i = 0; i < n; i++)
      System.out.print(nuts[i] + " ");
    System.out.println();
    for (int i = 0; i < n; i++)
      System.out.print(bolts[i] + " ");
  }
 
  // Driver code
  public static void main(String[] args)
  {
    char nuts[] = { '@', '#', '$', '%', '^', '&' };
    char bolts[] = { '$', '%', '&', '^', '@', '#' };
    int n = nuts.length;
    nutboltmatch(nuts, bolts, n);
  }
}
 
// This code is contributed by sanjeev2552


Python3
# Python3 program to implement
# above approach
# Hashmap based solution to
# solve
 
# Function to match nuts and
# bolts
def nutboltmatch(nuts,
                 bolts, n):
 
    hash1 = {}
 
    # creating a hashmap
    # for nuts
    for i in range(n):
        hash1[nuts[i]] = i
 
    # searching for nuts for
    # each bolt in hash map
    for i in range(n):
        if (bolts[i] in hash1):
            nuts[i] = bolts[i]
 
    # Print the result
    print("matched nuts and bolts are-")
    for i in range(n):
        print(nuts[i],
              end = " ")
    print()
    for i in range(n):
        print(bolts[i],
              end = " ")
 
# Driver code
if __name__ == "__main__":
 
    nuts = ['@', '#', '$',
            '%', '^', '&']
    bolts = ['$', '%', '&',
             '^', '@', '#']
    n = len(nuts)
    nutboltmatch(nuts, bolts, n)
 
# This code is contributed by Chitranayal


C#
// Hashmap based solution to solve
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // function to match nuts and bolts
  static void nutboltmatch(char[] nuts, char[] bolts, int n)
  {
    Dictionary hash = new Dictionary();
 
    // creating a hashmap for nuts
    for (int i = 0; i < n; i++)
    {
      hash.Add(nuts[i], i);
    }
 
    // searching for nuts for each bolt in hash map
    for (int i = 0; i < n; i++)
      if (hash.ContainsKey(bolts[i]))
        nuts[i] = bolts[i];
 
    // print the result
    Console.WriteLine("matched nuts and bolts are-");
    for (int i = 0; i < n; i++)
      Console.Write(nuts[i] + " ");
    Console.WriteLine();
    for (int i = 0; i < n; i++)
      Console.Write(bolts[i] + " ");
  }
 
  // Driver code
  static public void Main ()
  {
 
    char[] nuts = { '@', '#', '$', '%', '^', '&' };
    char[] bolts = { '$', '%', '&', '^', '@', '#' };
    int n = nuts.Length;
    nutboltmatch(nuts, bolts, n);
 
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出:

matched nuts and bolts are-
$ % & ^ @ # 
$ % & ^ @ # 

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