Cuckoo Hashing
来源:互联网 发布:龟牌极限蜡怎么样 知乎 编辑:程序博客网 时间:2024/04/30 06:41
Cuckoo Hashing
Time Limit:1000MS Memory Limit:65536K
Total Submit:3 Accepted:3
Description
One of the most fundamental data structure problems is the dictionary problem: given a set D of words you want to be able to quickly determine if any given query string q is present in the dictionary D or not. Hashing is a well-known solution for the problem. The idea is to create a function h : Σ* → [0..n-1] from all strings to the integer range 0, 1, .., n-1, i.e. you describe a fast deterministic program which takes a string as input and outputs an integer between 0 and n-1. Next you allocate an empty hash table T of size n and for each word w in D, you set T[h(w)] = w. Thus, given a query string q, you only need to calculate h(q) and see if T[h(q)] equals q, to determine if q is in the dictionary. Seems simple enough, but aren't we forgetting something? Of course, what if two words in D map to the same location in the table? This phenomenon, called collision, happens fairly often (remember the Birthday paradox: in a class of 24 pupils there is more than 50% chance that two of them share birthday). On average you will only be able to put roughly √n-sized dictionaries into the table without getting collisions, quite poor space usage!
A stronger variant is Cuckoo Hashing. The idea is to use two hash functions h1 and h2. Thus each string maps to two positions in the table. A query string q is now handled as follows: you compute both h1(q) and h2(q), and if T[h1(q)] = q, or T[h2(q)] = q, you conclude that q is in D. The name "Cuckoo Hashing" stems from the process of creating the table. Initially you have an empty table. You iterate over the words d in D, and insert them one by one. If T[h1(d)] is free, you set T[h1(d)] = d. Otherwise if T[h2(d)] is free, you set T[h2(d)] = d. If both are occupied however, just like the cuckoo with other birds' eggs, you evict the word r in T[h1(d)] and set T[h1(d)] = d. Next you put r back into the table in its alternative place (and if that entry was already occupied you evict that word and move it to its alternative place, and so on). Of course, we may end up in an infinite loop here, in which case we need to rebuild the table with other choices of hash functions. The good news is that this will not happen with great probability even if D contains up to n/2 words!
Input
On the first line of input is a single positive integer 1 ≤ t ≤ 50 specifying the number of test cases to follow. Each test case begins with two positive integers 1 ≤ m ≤ n ≤ 10000 on a line of itself, m telling the number of words in the dictionary and n the size of the hash table in the test case. Next follow m lines of which the ith describes the ith word di in the dictionary D by two non negative integers h1(di) and h2(di) less than n giving the two hash function values of the word di. The two values may be identical.
Output
For each test case there should be exactly one line of output either containing the string "successful hashing" if it is possible to insert all words in the given order into the table, or the string "rehash necessary" if it is impossible.
Sample Input
23 30 11 22 05 62 33 11 25 12 5
Sample Output
successful hashingrehash necessary
Source
= =!
A complex description just to explain a 二分匹配问题
m 个 points 装入 n 个 boxes,each point has two position to chooce, ask whether can make each point a position to stay.
- Cuckoo Hashing
- hd 1672 Cuckoo Hashing
- HDU-1672-Cuckoo Hashing
- csu:1887: Cuckoo Hashing
- B(1887): Cuckoo Hashing
- Cuckoo Hashing :二分匹配问题
- Cuckoo for Hashing(hash)
- 南邮 OJ 1523 Cuckoo Hashing
- Cuckoo for Hashing--关于哈希表的练习
- HNU 13064 Cuckoo for Hashing解题报告 North America - East Central 2013
- cuckoo framework
- Cuckoo Search
- Cuckoo Sandbox
- Hashing
- Hashing
- Hashing
- Hashing
- Hashing
- 非典型2D游戏引擎 Orx 源码阅读笔记(3) 内存管理
- struts2学习笔记(一)
- Android 无线启动过程分析 无线启动过程分析
- 学生管理系统小感
- 最简单的CI框架入门示例--数据库取数据
- Cuckoo Hashing
- HDU 1757(数论,矩阵)
- 测试员与开发员的区别
- 例子:BlackBerry真正的后台运行程序,Task里面看不到的哦
- 传谷歌预推新手机 Nexus Two下月发布
- 汇编 数据结构--数组
- 北京的六条经典徒步线路
- FTP两种工作模式:主动模式(Active FTP)和被动模式
- 跨dll,针对Vector引用的传递没有问题,但是Map就出现问题了