perl 哈希数组的哈希_使用哈希检查两个数组是否相似

发布时间:2023-07-09 11:00

perl 哈希数组的哈希

Prerequisite: Hashing data structure

先决条件: 哈希数据结构

Problem statement:

问题陈述:

Check whether two arrays are similar or not using the hash table. The arrays are of the same size.

使用哈希表检查两个数组是否相似。 数组的大小相同。

Example:

例:

arr1= [1, 2, 1, 3, 2, 1]
arr2= [2, 2, 3, 1, 1, 1]

Solution:

解:

There are several ways to solving this problem and one is by sorting both of the array. Then we can check elements one by one and if the two arrays are similar, it has to match for every single element. So, after sorting, a[i] must be b[i] for each i. But the method we will discuss is hashing which computes more easily.

解决此问题的方法有几种,一种是对两个数组进行排序。 然后我们可以一一检查元素,如果两个数组相似,则必须为每个元素匹配。 因此,排序后,每个i的 a [i]必须是b [i] 。 但是我们将讨论的方法是散列,它更容易计算。

The approach is to create two separate hash tables for each array. If the hash tables are exactly same then we can say that the arrays are exactly same.

该方法是为每个数组创建两个单独的哈希表。 如果哈希表完全相同,那么我们可以说数组完全相同。

So how can we create the hash tables and what will be the hash function?

那么我们如何创建哈希表,哈希函数将是什么呢?

Okay, so here each element is our key and the hash table has size of the range of the array. So, if the range of the array is [0,99] then the hash table size would be 100.
What will be our hash function and how would we map the keys to the corresponding location in the hash table?

好的,因此这里的每个元素都是我们的键,哈希表具有数组范围的大小。 因此,如果数组的范围为[0,99],则哈希表大小将为100
我们的哈希函数将是什么?如何将键映射到哈希表中的对应位置?

The hash function h(x)=x here but instead of storing the key itself using linear probing we will keep the frequency (this is same as the number of collisions) only as it does not make any sense to use linear probing as each unique key will have a unique location.

哈希函数h(x)= x ,但是我们不会使用线性探测来存储密钥本身,而是将频率保持不变(这与碰撞次数相同),只是因为将线性探测用作每个唯一值没有意义键将具有唯一的位置。

So, for example, if we have three 2s as our key, the hash table will store the count (frequency) at location 2.

因此,例如,如果我们有三个2作为密钥,则哈希表将在位置2存储计数(频率)。

So, using the above hash function, we will create two separate hash tables for two arrays (The hash function will remain the same for both)

因此,使用上面的哈希函数,我们将为两个数组创建两个单独的哈希表(哈希函数对于两个数组将保持相同)

So the algorithm will be,

因此算法将是

Step 1:

第1步:

Create the hash table like below:
Initially hash tables are empty (Hash1 & Hash2)

For each key in input array arr1:
Hash1[key]++

For each key in input array arr2:
Hash2[key]++

Step 2:

第2步:

Compare each location of the hash table
If all locations have the same value (same frequency for each unique key) 
then the two arrays are the same otherwise not.

Dry run with the example:

空运行示例:

arr1= [1, 2, 1, 3, 2, 1]
arr2= [2, 2, 3, 1, 1, 1]

After creating the hash table as step1 we will have

Hash1:
Index	Value
1	3
2	2
3	1
  
Hash2:
Index	Value
1	3
2	2
3	1

So, 
both hash1 and hash 2 is exactly 
the same and thus the arrays are same too.

Another example where arrays are not exactly same,
arr1= [1, 2, 1, 3, 2, 3]
arr2= [2, 2, 3, 1, 1, 1]

After creating the hash table as step1 we will have
Hash1:
Index	Value
1	2
2	2
3	2
  
Hash2:
Index	Value
1	3
2	2
3	1

Since location 1 has different values for hash1 and hash2 
we can conclude the arrays are not the same.
So, what we actually did using hashing is to check 
whether all the unique elements in the two arrays are exactly 
the same or not and if the same then their 
frequencies are the same or not.

C++ implementation:

C ++实现:

//C++ program to check if two arrays 
//are equal or not

#include 
using namespace std;

bool similar_array(vector arr1, vector arr2)
{
    //create teo different hash table where for each key 
    //the hash function is h(arr[i])=arr[i]
    //we will use stl map as hash table and 
    //will keep frequency stored
    //so say two keys arr[0] and arr[5] are mapping to 
    //the same location, then the location will have value 2
    //instead of the keys itself
    //if two hash tables are exactly same then 
    //we can say that our arrays are similar
    map hash1;
    map hash2;

    //for each number
    for (int i = 0, j = 0; i < arr1.size(); i++, j++) {
        hash1[arr1[i]]++;
        hash2[arr2[i]]++;
    }

    //now check whether hash tables are exactly same or not
    for (auto it = hash1.begin(), ij = hash2.begin(); it != hash1.end() && ij != hash2.end(); it++, ij++) {
        if (it->first != ij->first || it->second != ij->second)
            return false;
    }

    //so ans will be updated maxdiff
    return true;
}

int main()
{
    int n, m;
 
    cout << \"Enter number of elements for array1 & array2\\n\";
    cin >> n;

    //arrays having equal sum
    vector arr1(n, 0);
    vector arr2(n, 0);
    cout << \"Input the array elements\\n\";

    for (int i = 0; i < n; i++) {
        cin >> arr1[i];
        cin >> arr2[i];
    }
 
    if (similar_array(arr1, arr2))
        cout << \"The arrys are equal\";
    else
        cout << \"The arrys are not equal\";

    return 0;        
}

Output:

输出:

Enter number of elements for array1 & array2
6
Input the array elements
1 2 1 3 2 1
2 2 3 1 1 1
The arrys are equal


翻译自: https://www.includehelp.com/data-structure-tutorial/check-if-two-arrays-are-similar-or-not-using-hashing.aspx

perl 哈希数组的哈希

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号