Cuckoo Hashing

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

2
3 3
0 1
1 2
2 0
5 6
2 3
3 1
1 2
5 1
2 5

Sample Output

successful hashing
rehash necessary

裸2SAT

相同值的位置表示为 !A or !B 即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define M 40005
using namespace std;
int all,be[],n,m,x,y;
int dfn[M],low[M],instack[M],belong[M],stack[M],stak,curr,num;
int e[M],ne[M],ee[M];
vector<int> vec[]; void add(int x,int y){
e[all]=y;
ee[all]=x;
ne[all]=be[x];
be[x]=all++;
}
void tarjan(int x){
instack[x]=;
stack[++stak]=x;
dfn[x]=low[x]=++curr;
for(int j=be[x];j!=-;j=ne[j])
if(!dfn[e[j]]){
tarjan(e[j]);
if(low[x]>low[e[j]]) low[x]=low[e[j]];
}else if(instack[e[j]]&&low[x]>low[e[j]])
low[x]=low[e[j]];
if(dfn[x]==low[x]){
int j;
++num;
do{
j=stack[stak--];
instack[j]=;
belong[j]=num;
}while(j!=x);
}
}
int solve(){
curr=stak=num=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(instack,,sizeof(instack));
for(int i=;i<*n;i++)
if(!dfn[i]) tarjan(i);
bool flag=;
for(int i=;i<n;i++)
if(belong[*i]==belong[*i+]){
flag=;
break;
}
return flag;
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
for(int i=; i<=; i++)
vec[i].clear();
all=;
memset(be,-,sizeof(be));
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
{
scanf("%d%d",&x,&y);
for(vector<int>::iterator it=vec[x].begin(); it!=vec[x].end(); it++)
{
add(*it,*i+);
add(*i,(*it)^);
}
for(vector<int>::iterator it=vec[y].begin(); it!=vec[y].end(); it++)
{
add(*it,*i);
add(*i+,(*it)^);
}
vec[x].push_back(*i);
vec[y].push_back(*i+);
}
// for(int i=0; i<all; i++)
// printf("%d %d\n",ee[i],e[i]);
if(!solve()) printf("successful hashing\n");
else printf("rehash necessary\n");
}
return ;
}

看网上题解也可以用二分图匹配做,顺便写写练手

#include <cstdio>
#include <cstring>
#define M 80005
struct Edge{
int y,ne;
}e[M];
int be[M],pre[M],all,x,y,n,m;
bool vis[M];
void add(int x, int y)
{
e[all].y=y;
e[all].ne=be[x];
be[x]=all++;
}
void init()
{
all=;
memset(be,-,sizeof(be));
memset(pre,-,sizeof(pre));
}
bool dfs(int u)
{
for(int i=be[u]; i!=-; i=e[i].ne)
{
int v=e[i].y;
if(!vis[v])
{
vis[v]=;
if(pre[v]==- || dfs(pre[v]))
{
pre[v]=u;
return ;
}
}
}
return ;
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
init();
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
{
scanf("%d%d",&x,&y);
add(i,x+n);
add(i,y+n);
}
int ans=;
bool flag=;
for(int i=; i<n; i++)
{
memset(vis,,sizeof(vis));
if(!dfs(i))
{
flag=;
break;
}
}
if(flag) printf("successful hashing\n");
else printf("rehash necessary\n");
}
return ;
}

HDU 1672 Cuckoo Hashing的更多相关文章

  1. Cuckoo for Hashing(hash)hunnuoj

    Problem B:Cuckoo for HashingAn integer hash table is a data structure that supports insert, delete a ...

  2. Cuckoo for Hashing_双哈希表

    问题 B: Cuckoo for Hashing 时间限制: 1 Sec  内存限制: 64 MB提交: 24  解决: 12[提交][状态][讨论版] 题目描述 An integer hash ta ...

  3. Cuckoo hash算法分析——其根本思想和bloom filter一致 增加hash函数来解决碰撞 节省了空间但代价是查找次数增加

    基本思想: cuckoo hash是一种解决hash冲突的方法,其目的是使用简单的hash 函数来提高hash table的利用率,同时保证O(1)的查询时间 基本思想是使用2个hash函数来处理碰撞 ...

  4. Locality-sensitive hashing Pr[m(Si) = m(Sj )] = E[JSˆ (Si, Sj )] = JS(Si, Sj )

    A hash function that maps names to integers from 0 to 15. There is a collision between keys "Jo ...

  5. Java数据结构与算法解析(十二)——散列表

    散列表概述 散列表就是一种以 键-值(key-indexed) 存储数据的结构,我们只要输入待查找的值即key,即可查找到其对应的值. 散列表的思路很简单,如果所有的键都是整数,那么就可以使用一个简单 ...

  6. 《大数据日知录》读书笔记-ch3大数据常用的算法与数据结构

    布隆过滤器(bloom filter,BF): 二进制向量数据结构,时空效率很好,尤其是空间效率极高.作用:检测某个元素在某个巨量集合中存在. 构造: 查询: 不会发生漏判(false negativ ...

  7. CMU Database Systems - Indexes

    这章主要描述索引,即通过什么样的数据结构可以更加快速的查询到数据 介绍Hash Tables,B+tree,SkipList 以及索引的并行访问 Hash Tables hash tables可以实现 ...

  8. 二. 大数据常用的算法和数据结构 <<大数据日知录>> 读书笔记

    基本上是hash实用的各种举例 布隆过滤器 Bloom Filter 常用来检测某个原色是否是巨量数据集合中的成员,优势是节省空间,不会有漏判(已经存在的数据肯定能够查找到),缺点是有误判(不存在的数 ...

  9. Go语言实现布谷鸟过滤器

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com/archives/453 介绍 在我们工作中,如果遇到如网页 URL 去重.垃圾邮件识别 ...

随机推荐

  1. 中国IT人,你们是否从没想过开发一款伟大的产品?

    我也是今年刚毕业的,一毕业就做了猎头,从开始实习到正式工作,迄今为止接触的IT技术人不下上千人了.这里面有腾讯.阿里巴巴.百度.360.金山.金蝶.用友.华为.惠普等从事自主研发的大牛,也有很多软通. ...

  2. 你所不知道的黑客工具之 EK 篇

    EK(Exploit kits)是指一套利用恶意软件感染用户电脑发起攻击的黑客工具,时下最著名的有 Angler EK.Fiesta EK.Hanjuan EK.Nuclear EK.Neutrino ...

  3. Web Server 和 HTTP 协议

    https://toutiao.io/posts/xm2fr/preview 一直在找实习,有点什么东西直接就在evernote里面记了,也没时间来更新到这里.找实习真是个蛋疼的事,一直找的是困难模式 ...

  4. POJ3267The Cow Lexicon

    http://poj.org/problem?id=3267 题意 : 给你一个message,是给定字符串,然后再给你字典,让你将message与字典中的单词进行匹配,输出要删掉多少字母. 思路 : ...

  5. [SQL Server 系] T-SQL数据库的创建与修改

    创建数据库 USE master; GO CREATE DATABASE ToyUniverse ON ( NAME = ToyUniverse_Data, FILENAME = 'F:\Projec ...

  6. PKUSC 模拟赛 day1 上午总结

    思考了一下第二题,觉得有无数种乱搞做法 类似什么bitset压位,MCS染色之类奇怪的做法 然而都是玄学正确性或者玄学复杂度 先放题解把 第一题显然具有单调性,二分就可以啦 O(nlogn),貌似输出 ...

  7. 李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片

     李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片  源码:  // //  ViewController.m //  08-九宫格扩展 // //  Created by 李洪强 ...

  8. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...

  9. asp.net中当服务器出错时显示指定的错误页面

    http://blog.csdn.net/helloxiaoyu/article/details/2943537 此篇文章描述了当异常再ASP.NET中发生时怎样使用C#.NET代码去拦截和相应异常. ...

  10. JAVA! static什么作用?

    是静态修饰符,什么叫静态修饰符呢?大家都知道,在程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到程序退出内存才会释放这个空间,也就是只 ...