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的更多相关文章

  1. 哈希(Hash)与加密(Encrypt)相关内容

    1.哈希(Hash)与加密(Encrypt)的区别 哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可 ...

  2. 数据结构之哈希(hash)表

    最近看PHP数组底层结构,用到了哈希表,所以还是老老实实回去看结构,在这里去总结一下. 1.哈希表的定义 这里先说一下哈希(hash)表的定义:哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过 ...

  3. redis学习-散列表常用命令(hash)

    redis学习-散列表常用命令(hash)   hset,hmset:给指定散列表插入一个或者多个键值对 hget,hmget:获取指定散列表一个或者多个键值对的值 hgetall:获取所欲哦键值以及 ...

  4. 【Redis】命令学习笔记——哈希(hash)(15个超全字典版)

    本篇基于redis 4.0.11版本,学习哈希(hash)相关命令. hash 是一个string类型的field和value的映射表,特别适合用于存储对象. 序号 命令 描述 实例 返回 HSET ...

  5. 《数据结构与算法分析——C语言描述》ADT实现(NO.05) : 散列(Hash)

    散列(Hash)是一种以常数复杂度实现查找功能的数据结构.它将一个关键词Key,通过某种映射(哈希函数)转化成索引值直接定位到相应位置. 实现散列有两个关键,一是哈希函数的选择,二是冲突的处理. 对于 ...

  6. 一致性哈希算法(consistent hashing)(转)

    原文链接:每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)  一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网 ...

  7. 哈希表(hash)详解

     哈希表结构讲解: 哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度. ...

  8. 哈希(hash)理解

    转载自https://www.cnblogs.com/mingaixin/p/4318837.html 一.什么是哈希?(一种更复杂的映射) Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就 ...

  9. ruby 对象转换哈希(Hash)

    通过 ActiveRecord 从数据库的某张数据表(table)中获取的对象如何转换成为 Hash orders_table 是一张订单信息表,对应的 model 为 Orders @order = ...

随机推荐

  1. 201621123018《Java程序设计》第6周学习报告

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 2. 书面作业 1. clone方法 1.1 在 ...

  2. 连接dubbox注册中心的端口默认是20880

    pyg服务工程  都没用到20880 都没用默认,从20881 开始用,第二个服务工程 的则为20882 <dubbo:protocol name="dubbo" port= ...

  3. mysql 存储过程 游标嵌套

    基本表temp 包括 name, type, sendCity, getCity 分别对应物流送货司机名, 倒车的第几段, 发货城市, 收货城市 表结构 -- -------------------- ...

  4. Docker端口映射(六)

    一.容器端口映射 1.1. 外部访问容器 在启动容器时候,如果不指定参数,在容器外部是无法通过网络来访问容器内的服务的 当容器运行一些网络服务的时候,我们可以通过指定-p或者-P参数来实现能够让外部访 ...

  5. Django 模版过滤器

    模版常用过滤器 在模版中,有时候需要对一些数据进行处理以后才能使用.一般在Python中我们是通过函数的形式来完成的.而在模版中,则是通过过滤器来实现的.过滤器使用的是|来使用.比如使用add过滤器, ...

  6. How To Scan QRCode For UWP (1)

    本文将介绍实现一个类似于微信扫一扫功能的UI界面,后续会再实现具体的识别二维码的功能. 实例使用的Win10 SDK Version是Windows 10 Anniversary Edition(10 ...

  7. windows 64位下 Octave 不能画图的解决办法

    如果不能画图,可能需要更改图形工具包. 1.首先,查看当前的工具包.在Octave命令行中键入 graphics_toolkit,结果如下: >> graphics_toolkit    ...

  8. Hadoop环境搭建及wordcount程序

    目的: 前期学习了一些机器学习基本算法,实际企业应用中算法是核心,运行的环境和数据处理的平台是基础. 手段: 搭建简易hadoop集群(由于机器限制在自己的笔记本上通过虚拟机搭建) 一.基础环境介绍 ...

  9. 记一次TCP重发接口调用的问题

    问题描述:基于微软RDP协议,使用开源rdp库与微软skpye软件进行基于tcp的p2p通讯,由于rdp协议传输原始图片数据较大,调用公司内部ice p2p通讯接口处会导致失败. 错误思路:一开始是怀 ...

  10. ASP.NET MVC 与NLog的使用

    NLog是一个.NET 下一个完善的日志工具,个人已经在项目中使用很久,与ELMAH相比,可能EAMAH更侧重 APS.NET MVC 包括调试路由,性能等方面,而NLog则更简洁. github: ...