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. 在Windows7系统上能正常使用的程序,Windows10运行后部分状态不能及时变更

    这是最近在开发一个通信项目时遇到的问题,一开始以为是窗体样式的原因,把窗体换成系统窗体之后还是在Win10上不能正常使用,后面突然想到会不会是匹配原因,试了一下,结果真的就正常了. 问题:例如一个通信 ...

  2. .net图表之ECharts随笔06-这才是最简单的

    今天搞柱形图的时候,发现了一个更简单的用法.那就是直接使用带all的那个js文件 基本步骤: 1.为ECharts准备一个具备大小(宽高)的Dom 2.ECharts的js文件引入(echarts-a ...

  3. intellij 引入本地库并war打包

    一.引入本地库 1.File -> Project Structure -> Libraries,点击+,新增本地lib库. 2.File -> Project Structure ...

  4. Vue.js之下拉列表及选中触发事件

    老早就听说了Vue.js是多么的简单.易学.好用等等,然而我只是粗略的看了下文档,简单的敲了几个例子,仅此而已. 最近由于项目的需要,系统的看了下文档,也学到了一些东西. 废话不多说,这里要说的是下拉 ...

  5. PCA (主成分分析)详解 (写给初学者) 结合matlab(转载)

    一.简介 PCA(Principal Components Analysis)即主成分分析,是图像处理中经常用到的降维方法,大家知道,我们在处理有关数字图像处理方面的问题时,比如经常用的图像的查询问题 ...

  6. Dynamo(Amazon分布式存储引擎)

    参考资料: http://blog.csdn.net/zgl_dm/article/details/6291984 http://blog.csdn.net/cywosp/article/detail ...

  7. 常见数据结构的Java实现

    单链表的Java实现 首先参考wiki上的单链表说明,单链表每个节点包含数据和指向链表中下一个节点的指针或引用.然后看代码 import java.lang.*; public class Singl ...

  8. 虚幻4引擎角色蓝图Character的Movement组件学习

    Jumping/Falling Air Control :角色在空中时的控制参数.数值为1 代表完全控制. Air Control Boost Multiplier :当角色的速度超过 Velocit ...

  9. 高可用Hadoop平台-运行MapReduce程序

    1.概述 最近有同学反应,如何在配置了HA的Hadoop平台运行MapReduce程序呢?对于刚步入Hadoop行业的同学,这个疑问却是会存在,其实仔细想想,如果你之前的语言功底不错的,应该会想到自动 ...

  10. 2015年第六届蓝桥杯C/C++程序设计本科B组决赛 完美正方形

    完美正方形 如果一些边长互不相同的正方形,可以恰好拼出一个更大的正方形,则称其为完美正方形.历史上,人们花了很久才找到了若干完美正方形.比如:如下边长的22个正方形 2 3 4 6 7 8 12 13 ...