Cuckoo for Hashing(hash)hunnuoj
Problem B:
Cuckoo for Hashing
An integer hash table is a data structure that supports insert, delete and lookup of integer values in
constant time. Traditional hash structures consist of an array (the hash table) of some size n, and a
hash function f(x) which is typically f(x) = x mod n. To insert a value x into the table, you compute
its hash value f(x) which serves as an index into the hash table for the location to store x. For example,
if x = 1234 and the hash table has size 101, then 1234 would be stored in location 22 = 1234 mod 101.
Of course, it’s possible that some other value is already stored in location 22 (x = 22 for example),
which leads to a collision. Collisions can be handled in a variety of ways which you can discuss with
your faculty advisor on the way home from the contest.
Cuckoo hashing is a form of hashing that employs two hash tables T1and T2, each with its own hash
function f1(x) and f2(x). Insertion of a value x proceeds as follows: you first try to store x in T1using
f1(x). If that location is empty, then simply store x there and you’re done. Otherwise there is a collision
which must be handled. Let y be the value currently in that location. You replace y with x in T1, and
then try to store y in T2 using f2(y). Again, if this location is empty, you store y there and you’re
done. Otherwise, replace the value there (call it z) with y, and now try to store z back in T1using
f1(z), and so on. This continues, bouncing back and forth between the two tables until either you find
an empty location, or until a certain number of swaps have occurred, at which point you rehash both
tables (again, something to discuss with your faculty advisor). For the purposes of this problem, this
latter occurrence will never happen, i.e., the process should always continue until an empty location is
found, which will be guaranteed to happen for each inserted value.
Given the size of the two tables and a series of insertions, your job is to determine what is stored in
each of the tables.
(For those interested, cuckoo hashing gets its name from the behavior of the cuckoo bird, which is
known to fly to other bird’s nests and lay its own eggs in it alongside the eggs already there. When
the larger cuckoo chick hatches, it pushes the other chicks out of the nest, thus getting all the food for
itself. Gruesome but efficient.)
Input
Input for each test case starts with 3 positive integers n1n2m, where n1and n2are the sizes of the
tables T1 and T2 (with n1,n2 ≤ 1000 and n1 6= n2) and m is the number of inserts. Following this
will be m integer values which are the values to be inserted into the tables. All of these values will be
non-negative. Each table is initially empty, and table Tiuses the hash function fi(x) = x mod ni. A
line containing 3 zeros will terminate input.
Output
For each test case, output the non-empty locations in T1followed by the non-empty locations in T2.
Use one line for each such location and the form i:v, where i is the index location of the table, and v
is the value stored there. Output values in each table from lowest index to highest. If either table is
empty, output nothing for that table.
2013 East Central Regional Contest
4
Sample Input
5 7 4
8 18 29 4
6 7 4
8 18 29 4
1000 999 2
1000
2000
0 0 0
Sample Output
Case 1:
Table 1
3:8
4:4
Table 2
1:29
4:18
Case 2:
Table 1
0:18
2:8
4:4
5:29
Case 3:
Table 1
0:2000
Table 2
1:1000
题意 :意思就是哈希来的,具体大意就是说有两个哈希表,然后有这样一组数据,
让你把这组数据存到这两个哈希表里,然后不能重复,先让数据往表1里存,就是对
表1的长度进行取余,如果余数这个位置没有数就存上,如果有的话,就存上这个数,
让原来的数再去表2里存,也是按照这个方式。就是来回踢。。。我觉得。。。。
//思路:两个哈希表,一个循环找即可。。。
#include <stdio.h>
#include <string.h>
#include <map>
#include <iostream> using namespace std ;
int ch[];
int sh[];
int main()
{
int n1,n2,k ;
int t = ;
while(scanf("%d%d%d",&n1,&n2,&k)!=EOF)
{
if(n1 == &&n2==&&k==) break;
memset(ch,-,sizeof(ch));
memset(sh,-,sizeof(sh)) ; int x ;
for(int i = ; i <= k ; i++)
{
scanf("%d",&x);
while()
{
int s=x%n1;
if(ch[s] == -)
{
ch[s]=x;
break;
}
else
{
int temp=ch[s];
ch[s]=x;
int tt=temp%n2;
if(sh[tt]==-)
{
sh[tt]=temp;
break;
}
else
{
x=sh[tt];
sh[tt]=temp;
}
}
}
}
printf("Case %d:\n",t);
t++;
int flag = ;
for(int i = ; i < n1 ; i++)
{
if(ch[i] != -)
{
flag = ;
break ;
}
}
if(flag)
{
printf("Table 1\n");
for(int i = ; i < n1 ; i++)
{
if(ch[i] != -)
{
printf("%d:%d\n",i,ch[i]);
}
}
}
flag = ;
for(int i = ; i < n2 ; i++)
{
if(sh[i] != -)
{
flag = ;
break ;
}
}
if(flag)
{
printf("Table 2\n");
for(int i = ; i < n2 ; i++)
{
if(sh[i] != -)
{
printf("%d:%d\n",i,sh[i]) ;
}
}
}
}
return ;
}
Cuckoo for Hashing(hash)hunnuoj的更多相关文章
- 哈希(Hash)与加密(Encrypt)相关内容
1.哈希(Hash)与加密(Encrypt)的区别 哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可 ...
- 数据结构之哈希(hash)表
最近看PHP数组底层结构,用到了哈希表,所以还是老老实实回去看结构,在这里去总结一下. 1.哈希表的定义 这里先说一下哈希(hash)表的定义:哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过 ...
- redis学习-散列表常用命令(hash)
redis学习-散列表常用命令(hash) hset,hmset:给指定散列表插入一个或者多个键值对 hget,hmget:获取指定散列表一个或者多个键值对的值 hgetall:获取所欲哦键值以及 ...
- 【Redis】命令学习笔记——哈希(hash)(15个超全字典版)
本篇基于redis 4.0.11版本,学习哈希(hash)相关命令. hash 是一个string类型的field和value的映射表,特别适合用于存储对象. 序号 命令 描述 实例 返回 HSET ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.05) : 散列(Hash)
散列(Hash)是一种以常数复杂度实现查找功能的数据结构.它将一个关键词Key,通过某种映射(哈希函数)转化成索引值直接定位到相应位置. 实现散列有两个关键,一是哈希函数的选择,二是冲突的处理. 对于 ...
- 一致性哈希算法(consistent hashing)(转)
原文链接:每天进步一点点——五分钟理解一致性哈希算法(consistent hashing) 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网 ...
- 哈希表(hash)详解
哈希表结构讲解: 哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度. ...
- 哈希(hash)理解
转载自https://www.cnblogs.com/mingaixin/p/4318837.html 一.什么是哈希?(一种更复杂的映射) Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就 ...
- ruby 对象转换哈希(Hash)
通过 ActiveRecord 从数据库的某张数据表(table)中获取的对象如何转换成为 Hash orders_table 是一张订单信息表,对应的 model 为 Orders @order = ...
随机推荐
- WPF ListBox的进阶使用(二)
项目中经常使用需要根据搜索条件查询数据,然后用卡片来展示数据.用卡片展示数据时,界面的宽度发生变化,希望显示的卡片数量也跟随变化.WrapPanel虽然也可以实现这个功能,但是将多余的部分都留在行尾, ...
- 【五校联考3day2】C
題意: 現有一平面直角坐標系,有n個點,每一個點必須向某一個方向發射射線,且任意一條射線必須與某一條坐標軸平行.定義一種發射射線的方案是合法的,則方案必須滿足: 1.沒有一條射線交叉 2.沒有一條射線 ...
- SVM的基础原理
因为看cs231的时候用了一下multi-class的svm,所以又把svm给复习了一下,教材是周志华的西瓜书,这里是大概的笔记. 1.线性可分 对于一个数据集: 如果存在一个超平面X能够将D中的正负 ...
- js获取n分钟(或n小时或n个月)后(或前)的时间(日期)
标题有点绕,其实意思就是根据系统当前时间,获取n分钟或n小时或n个月后的时间. 例如:当前时间下,获取10分钟后的时间. var date=new Date(); //1. js获取当前时间 var ...
- linux定时重启tomcat服务的脚本学习
要求:在linux中定时重启一个tomcat服务 一:shell脚本即Shell Script [1],Shell脚本与Windows/Dos下的批处理相似,也就是用各类命令预先放入到一个文件中,方便 ...
- python 结巴分词简介以及操作
中文分词库:结巴分词 文档地址:https://github.com/fxsjy/jieba 代码对 Python 2/3 均兼容 全自动安装:easy_install jieba 或者 pip in ...
- Nodejs的模块系统
global对象 浏览器端JavaScript中的全局对象为"window",在浏览器中定义的变量都会成为"window"对象的属性. 不像浏览器端JavaSc ...
- 手推SVM
推不动了,改日再更!
- 【disruptor】2、disruptor中生产者线程与消费者之间的协调
由于ringbuffer是一个环形的队列,那么生产者和消费者在遍历这个队列的时候,如何制衡呢? 1.生产快,消费慢,数据丢失? 生产者速度过快,导致一个对象还没消费完,就循环生产了一个新的对象要加入r ...
- vue教程2-03 vue计算属性的使用 computed
vue教程2-03 vue计算属性的使用 computed computed:{ b:function(){ //默认调用get return 值 } } ---------------------- ...