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. JVM调优推荐

    此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.JVM的调优主要是内存的调优,主要调两个方面: 各个代的大小 垃圾收集器选择 2.各个代的大小 常用的 ...

  2. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. C# Winform下一个热插拔的MIS/MRP/ERP框架(简介)

    Programmer普弱哥们都喜欢玩自己的框架,我也不例外. 理想中,这个框架要易于理解.易于扩展.易于维护:最重要的,易于CODING. 系统是1主体框架+N模组的多个EXE/DLL组成的,在主体框 ...

  4. UICollectionView设置首个cell默认选中(二)

    上篇对于UICollectionView默认选中cell采取的是每个cell分别对应一个标识,也就代表着废除了UICollectionView的重用机制.对于较少的数据情况是可以的,但是对于数据比较大 ...

  5. 交换机与VLAN

    1. 交换机 1.1 定义 交换机是一种基于MAC地址识别,能完成封装转发数据帧功能的网络设备. HUB集线器是一种物理层共享设备,HUB本身不能识别MAC 地址和IP地址,当同一局域网内的A主机给B ...

  6. 爬虫 解码gb1312类型

    今天我爬虫的时候竟然遇见了网页编码为gb1312类型的网站  , 不是平常的utf-8 遇到这种类型的时候忽视它是最好的办法 ① respond.content.decode('gb18030','i ...

  7. flask_json数据入库Mongo

    首先我们先导入python内置的json库,用来将接送数据转换为python对象 import json #导入自定义的数据公共库 from db_tool import db #载入库之前先清空数据 ...

  8. Django模版结构优化和加载静态文件

    引入模版 有时候一些代码是在许多模版中都用到的.如果我们每次都重复的去拷贝代码那肯定不符合项目的规范.一般我们可以把这些重复性的代码抽取出来,就类似于Python中的函数一样,以后想要使用这些代码的时 ...

  9. hybird app混合开发介绍

    一 概念 1 Hybird App,是用现有前端(html,js,css)技术来开发的app.特点:1 灵活(开发灵活 ,部署灵活) 2 拥有类似原生的性能体验. 2 不是h5页面,也不是在webvi ...

  10. MVC3学习:利用mvc3+ajax实现全选和批量删除

    本例数据库操作使用EF code first; 先利用mvc自带的模板,先生成一个list视图,然后再手动添加复选框和删除按钮 <table> <tr> @*在标题行添加一个全 ...