perl 哈希數組的哈希_使用哈希檢查兩個數組是否相似

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 <bits/stdc++.h>
using namespace std;
bool similar_array(vector<int> arr1, vector<int> 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<int, int> hash1;
map<int, int> 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<int> arr1(n, 0);
vector<int> 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 哈希數組的哈希

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542650.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542650.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542650.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

codevs3872 郵遞員送信(SPFA)

郵遞員送信 時間限制: 1 Sec 內存限制: 64 MB提交: 10 解決: 5[提交][狀態][討論版] 題目描述 有一個郵遞員要送東西&#xff0c;郵局在節點1.他總共要送N-1樣東西&#xff0c;其目的地分別是2~N。由于這個城市的交通比較繁忙&#xff0c;因此所有的道路都是單行的&#xff0…

java上傳csv文件上傳_java處理csv文件上傳示例詳解

前言&#xff1a;示例只是做了一個最最基礎的上傳csv的示例&#xff0c;如果要引用到代碼中去&#xff0c;還需要根據自己的業務自行添加一些邏輯處理。readcsvutil工具類package com.hanfengyeqiao.gjb.utils;import java.io.*;import java.util.*;/*** csv工具類*/public cla…

360更新補丁一直提示正在安裝_遠程利用POC公布|CVE20200796:微軟發布SMBv3協議“蠕蟲級”漏洞補丁通告...

更多全球網絡安全資訊盡在邑安全www.eansec.com0x00 事件描述2020年3月11日&#xff0c;360CERT監測到有海外廠家發布安全規則通告&#xff0c;通告中描述了一處微軟SMBv3協議的內存破壞漏洞&#xff0c;編號CVE-2020-0796&#xff0c;并表示該漏洞無需授權驗證即可被遠程利用&…

字符串的回文子序列個數_計算給定字符串中回文子序列的數量

字符串的回文子序列個數Problem statement: 問題陳述&#xff1a; Given a string you have to count the total number of palindromic subsequences in the giving string and print the value. 給定一個字符串&#xff0c;您必須計算給定字符串中回文子序列的總數并打印該值…

Linux-破解rhel7-root密碼

破解7的密碼1.linux16 rd.break2.mount -o remount,rw /sysroot3.chroot /sysroot4.passwd5.touch /.autorelabelexitexit7版本grub菜單加密1.grub2-mkpasswd-pbkdf22.vi /etc/grub.d/40_customset superusers"root"password_pbkdf2 root grub.pbkdf2.sha512.10000.…

適配接口 java_【Java 設計模式】接口型模式--Adapter(適配器)模式

簡介&#xff1a;【Java設計模式】接口型模式–Adapter(適配器)模式Adapter模式的宗旨就是&#xff1a;向客戶提供接口&#xff0c;并使用現有的類所提供的服務&#xff0c;以滿足客戶的需求。 或者說&#xff0c;現在有classA的方法滿足客戶的部分要求&#xff0c;將另一部分需…

deepinu盤制作工具_u盤啟動盤制作工具怎么制作 u盤啟動盤制作工具制作方法【詳細步驟】...

在電腦城很多技術人員都會使用u盤裝系統的方法給用戶電腦安裝系統&#xff0c;他們是怎么操作的呢?其實很簡單&#xff0c;就是通過u盤啟動盤來安裝系統的。而u盤啟動盤是需要用 u盤啟動盤制作工具 來制作的。那么問題又來了&#xff0c;u盤啟動盤制作工具怎么制作呢?下面就給…

openstack私有云_OpenStack-下一代私有云的未來

openstack私有云The OpenStack project is an open source cloud computing platform for all types of clouds, which aims to be simple to implement, massively scalable, and feature rich. Developers and cloud computing technologists from around the world create t…

outlook2010客戶端無法預覽及保存word,excel問題

outlook2010客戶端遇到的EXCEL預覽及保存問題今天遇到了一個這樣的問題&#xff0c;outlook2010打開以后其他的excel都可以打開預覽及保存&#xff0c;這個excel無法預覽既保存&#xff0c;經查是outlook2010預覽及打開的緩存有限制&#xff0c;超過后就無法預覽了&#xff0c;…

python自動化框架pytest pdf_Python 自動化測試框架 unittest 和 pytest 對比

一、用例編寫規則1.unittest提供了test cases、test suites、test fixtures、test runner相關的類,讓測試更加明確、方便、可控。使用unittest編寫用例,必須遵守以下規則:(1)測試文件必須先import unittest(2)測試類必須繼承unittest.TestCase(3)測試方法必須以“test_”開頭(4…

freemarker的測試結果框架_java必背綜合知識點總結(框架篇)

框架篇一、Struts1的運行原理在啟動時通過前端總控制器ActionServlet加載struts-config.xml并進行解析&#xff0c;當用戶在jsp頁面發送請求被struts1的核心控制器ActionServlet接收&#xff0c;ActionServlet在用戶請求時將請求參數放到對應的ActionForm對象中的成員變量中&am…

Java SecurityManager checkPackageDefinition()方法與示例

SecurityManager類的checkPackageDefinition()方法 (SecurityManager Class checkPackageDefinition() method) checkPackageDefinition() method is available in java.lang package. checkPackageDefinition()方法在java.lang包中可用。 We call getProperty("package.d…

java容器詳解_詳解Java 容器(第①篇)——概覽

![](http://img.blog.itpub.net/blog/2020/04/02/9d89d3008962c127.png?x-oss-processstyle/bb)容器主要包括 Collection 和 Map 兩種&#xff0c;Collection 存儲著對象的集合&#xff0c;而 Map 存儲著鍵值對(兩個對象)的映射表。# 一、Collection![](https://upload-images…

python圖形界面庫哪個好_8個必備的Python GUI庫

Python GUI 庫有很多&#xff0c;下面給大家羅列常用的幾種 GUI庫。下面介紹的這些GUI框架&#xff0c;能滿足大部分開發人員的需要&#xff0c;你可以根據自己的需求&#xff0c;選擇合適的GUI庫。1. wxPython wxPython 是一個跨平臺的 GUI 工具集&#xff0c;是 Python 語言的…

為什么在Python中使用string.join(list)而不是list.join(string)?

join() is a string method and while using it the separator string iterates over an arbitrary sequence, forming string representations of each of the elements, inserting itself between the elements. join()是一個字符串方法&#xff0c;使用它時&#xff0c;分隔…

js的client、scroll、offset詳解與兼容性

clientWidth&#xff1a;可視區寬說明&#xff1a;樣式寬padding參考&#xff1a;js的client詳解 scrollTop : 滾動條滾動距離說明&#xff1a;chrome下他會以為滾動條是文檔元素的&#xff0c;所以需要做兼容&#xff1a;var scrollTop document.documentElement.scrollTop |…

88是python語言的整數類型_Python基礎數據類型題

Python基礎數據類型 題 考試時間&#xff1a;三個小時 滿分100分&#xff08;80分以上包含80分及格&#xff09; 1&#xff0c;簡述變量命名規范&#xff08;3分&#xff09;1.必須是字母&#xff0c;數字&#xff0c;下劃線的任意組合。 2.不能是數字開頭 3.不能是python中的關…

[轉載]使用awk進行數字計算,保留指定位小數

對于在Shell中進行數字的計算&#xff0c;其實方法有很多&#xff0c;但是常用的方法都有其弱點&#xff1a; 1、bc bc應該是最常用的Linux中計算器了&#xff0c;簡單方便&#xff0c;支持浮點。 [wangdongcentos715-node1 ~]$ echo 12 |bc 3 [wangdongcentos715-node1 ~]$ ec…

dcom配置_spring cloud 二代架構依賴組件 全配置放送

一 背景介紹先來看一下我們熟悉的第一代 spring cloud 的組件spring cloud 現在已經是一種標準了&#xff0c;各公司可以基于它的編程模型編寫自己的組件 &#xff0c;比如Netflix、阿里巴巴都有自己的一套通過spring cloud 編程模型開發的分布式服務組件 。Spring Cloud 二代組…

olap 多維分析_OLAP(在線分析處理)| OLAP多維數據集和操作

olap 多維分析In the previous article of OLAP, we have seen various applications of OLAP, Various types of OLAP, advantages, and disadvantages of OLAP. In this article, we will learn about the, 在OLAP的上一篇文章中&#xff0c;我們了解了OLAP的各種應用&#x…