博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
perl 哈希数组的哈希_使用哈希检查两个数组是否相似
阅读量:2527 次
发布时间:2019-05-11

本文共 4926 字,大约阅读时间需要 16 分钟。

perl 哈希数组的哈希

Prerequisite:

先决条件:

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 tableIf 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 haveHash1:Index	Value1	32	23	1  Hash2:Index	Value1	32	23	1So, 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 haveHash1:Index	Value1	22	23	2  Hash2:Index	Value1	32	23	1Since 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 & array26Input the array elements1 2 1 3 2 12 2 3 1 1 1The arrys are equal

翻译自:

perl 哈希数组的哈希

转载地址:http://gctzd.baihongyu.com/

你可能感兴趣的文章
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>