[Algorithm] Hashing for search
Hashing Process
关于hash本身,解决冲突是一个小重点,如下图。

代码实现分析
—— 定义HashTable类
一、数据结构
def __init__(self):
self.size = 11
self.slots = [None] * self.size
self.data = [None] * self.size
二、Hashing策略
注意,因“冲突”而导致的rehash不是原本的"key+1",而是"key的hash结果+1"。用的是”线性解决冲突“的策略。
def bashfunction(self, key, size):
return key%size def rehash(self, oldhash, size):
return (oldhash+1)%size
三、设置
理解的关键,有冲突时找下一个位置,
def put(self, key, data):
hashvalue = self.hashfunction(key, len(self.slots)) if self.slots[hashvalue] == None
# 第一次出现,则直接添加
self.slots[hashvalue] = key
self.data[hashvalue] = data
else:
if self.slots[hashvalue] == key:
#已经有了则“更新”数值
self.data[hashvalue] = data
else:
# 有值但key不是,说明“被占”了,那就循环直到”没冲突“时
nextslot = self.rehash(bashvalue, len(self.slots))
while self.slots[nextslot] != None and self.slots[nextslot] != key: # ”被占“:非空,且key不对
nextslot = self.rehash(nextslot, len(self.slots)) # 找到位置后,看位置的具体情况;
if self.slots[nextslot] == None:
# append new key.
self.slots[nextslot] = key
self.data[nextslot] = data
else:
# update existing key's value.
self.data[nextslot] = data
四、获取
值得注意的是:”没找到“的标示是又回到了原来的起始位置。这也是线性探测的特点。
def get(self, key):
startslot = self.hashfuncion(key, len(self.slots)) data = None
stop = False
found = False
position = startslot while self.slots[position] != None and not found and not stop:
if self.slots[position] == key:
found = True
data = self.data[position]
else:
# update 'position', 冲突解决之‘线性探测’
position = self.rehash(position, len(self.slots))
if position == startslot:
# 是真没有这个key
stop = True return data
五、类的 ”字典化“
def __getitem__(self, key):
return self.get(key) def __setitem__(self, key, data):
self.put(key, data)
一个简单的例子:
class Tag:
def __init__(self):
self.change={'python':'This is python'} def __getitem__(self, item):
print('这个方法被调用')
return self.change[item] a=Tag()
print(a['python'])
End.
[Algorithm] Hashing for search的更多相关文章
- PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突
Hashing - Average Search Time PAT-1145 需要注意本题的table的容量设置 二次探测,只考虑正增量 这里计算平均查找长度的方法和书本中的不同 #include&l ...
- PAT 1145 Hashing - Average Search Time [hash][难]
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of d ...
- [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)
1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...
- PAT_A1145#Hashing - Average Search Time
Source: PAT A1145 Hashing - Average Search Time (25 分) Description: The task of this problem is simp ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- hdu, KMP algorithm, linear string search algorithm, a nice reference provided 分类: hdoj 2015-07-18 13:40 144人阅读 评论(0) 收藏
reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...
- 1145. Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
随机推荐
- centos7.x 安装系统/配置网络/设置主机名
1.安装系统 系统的安装就不多说了,自行查找百度,如:https://www.cnblogs.com/wcwen1990/p/7630545.html 2.配置网络(局域网上网) 修改配置 ...
- 逆向破解之160个CrackMe —— 018
CrackMe —— 018 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...
- c++ 左移
maxval = (1 << d) - 1: d=8 意思是2^d-1,相当于1左移d位
- Liunx软件安装之MySQL
一.安装MySQL 1.1 配置 yum 源 centos 默认没有 MySQL 的 yum 源,所以需要先配置 yum 源. 1) 前往 官网,选择对应系统版本 2) 右键复制链接 3) 在 cen ...
- acm未解之谜-洛谷P1109学生分组
把每一组的学生个数调度到一个给定区间范围内: 看了一圈题解,大佬都对原因避而不答: #include <iostream> #include <algorithm> using ...
- CodeForces 1058 F Putting Boxes Together 树状数组,带权中位数
Putting Boxes Together 题意: 现在有n个物品,第i个物品他的位置在a[i],他的重量为w[i].每一个物品移动一步的代价为他的w[i].目前有2种操作: 1. x y 将第x的 ...
- CodeForces 402 E Strictly Positive Matrix
Strictly Positive Matrix 题解: 如果原来的 a[i][j] = 0, 现要 a[i][j] = 1, 那么等于 sum{a[i][k] + a[k][j]} > 1. ...
- lightoj 1060 - nth Permutation(组合数+贪心)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1060 题解:如果是不重复数的这些操作可以用康托展开的逆来求,如果是有重复数字出 ...
- There is No Alternative CSU - 2097 最小生成树
Description ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens reque ...
- yzoj2057 x 题解
题意:给出一个集合,要求把这个集合分成两部分,使得一个集合中的任一元素都与另一个集合的全部元素都两两互质 暴力 枚举每个元素O(n^2)再暴力判gcd=1,如果非1就放入不同集合内,用并查集维护联通块 ...