问题 B: Cuckoo for Hashing

时间限制: 1 Sec  内存限制: 64 MB
提交: 24  解决: 12
[提交][状态][讨论版]

题目描述

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 T1 and 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 T1 using 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 T1 using 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 for each test case starts with 3 positive integers n1 n2 m, where n1 and n2 are 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 Ti uses the hash function fi(x) = x mod ni. A line containing 3 zeros will terminate input.

输出

For each test case, output the non-empty locations in T1 followed 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.

样例输入

5 7 4
8 18 29 4
6 7 4
8 18 29 4
1000 999 2
1000
2000
0 0 0

样例输出

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 解题思路:
这个题并不难,只要弄懂题意就比较容易了,但是英语是一大障碍啊!!!多学英语!!!
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int main()
{
int n1,n2,m;
int a[];
int b=;
int bq=;
int h1[];
int h2[];
int cc=;
while(scanf("%d %d %d",&n1,&n2,&m)!=EOF&&m!=){
b=;
bq=;
memset(h1,,sizeof(h1));
memset(h2,,sizeof(h2));
for(int i=;i<m;i++){
scanf("%d",&a[i]);
}
for(int i=;i<m;i++){
int locat=a[i]%n1;
int locat2=a[i]%n2;
if(b== && h1[locat]!=){
int t=h1[locat];
h1[locat]=a[i];
a[i]=t;
i--;
b=;
continue;
}
if(b== && h1[locat]==){
h1[locat]=a[i];
if(bq==){
bq=;
b=;
}else{
bq=;
b=;
} continue;
}
if(b== && h2[locat2]!=){
int t=h2[locat2];
h2[locat2]=a[i];
a[i]=t;
i--;
b=;
continue;
}
if(b== && h2[locat2]==){
h2[locat2]=a[i];
if(bq==){
bq=;
b=;
}else{
bq=;
b=;
}
continue;
} }
printf("Case %d:\n",cc+);
int cou=;
for(int i=;i<n1;i++){
if(h1[i]!=){
cou++;
break;
}
}
if(cou!=){
printf("Table 1\n");
for(int i=;i<n1;i++){
if(h1[i]!=){
printf("%d:%d\n",i,h1[i]);
}
}
} cou=;
for(int i=;i<n2;i++){
if(h2[i]!=){
cou++;
break;
}
}
if(cou!=){
printf("Table 2\n");
for(int i=;i<n2;i++){
if(h2[i]!=){
printf("%d:%d\n",i,h2[i]);
}
}
}
cc++;
}
return ;
} /**************************************************************
Problem: 2692
User: zz13
Language: C++
Result: 正确
Time:0 ms
Memory:1700 kb
****************************************************************/

Cuckoo for Hashing_双哈希表的更多相关文章

  1. Redis源码研究:哈希表 - 蕫的博客

    [http://dongxicheng.org/nosql/redis-code-hashtable/] 1. Redis中的哈希表 前面提到Redis是个key/value存储系统,学过数据结构的人 ...

  2. 漫谈Linux内核哈希表(1)

    关于哈希表,在内核里设计两个很重要的数据结构:    哈希链表节点: 点击(此处)折叠或打开 .x [include/linux/types.h]*/ struct hlist_node { stru ...

  3. Java基础知识强化之集合框架笔记75:哈希表

    1. 哈希表数据结构(数组): 2. 哈希表确定元素是否相同: (1)判断的是两个元素的哈希值是否相同                     如果相同,再判断两个对象内容是否相同 (2)判断哈希值相 ...

  4. perl5 第九章 关联数组/哈希表

    第九章 关联数组/哈希表 by flamephoenix 一.数组变量的限制二.定义三.访问关联数组的元素四.增加元素五.创建关联数组六.从数组变量复制到关联数组七.元素的增删八.列出数组的索引和值九 ...

  5. 开地址哈希表(Hash Table)的接口定义与实现分析

    开地址哈希函数的接口定义 基本的操作包括:初始化开地址哈希表.销毁开地址哈希表.插入元素.删除元素.查找元素.获取元素个数. 各种操作的定义如下: ohtbl_init int ohtbl_init ...

  6. 开地址哈希表(Hash Table)的原理描述与冲突解决

    在开地址哈希表中,元素存放在表本身中.这对于某些依赖固定大小表的应用来说非常有用.因为不像链式哈希表在每个槽位上有一个"桶"来存储冲突的元素,所以开地址哈希表需要通过另一种方法来解 ...

  7. Hash之哈希表的详解

    Hash算法 Hash算法的原理; 决绝冲突的办法是: 线性探查法; 双散列函数法; 拉链法处理碰撞; 哈希原理及实现; 哈希表-Hash table, 也叫散列表;

  8. 图书管理(Loj0034)+浅谈哈希表

    图书管理 题目描述 图书管理是一件十分繁杂的工作,在一个图书馆中每天都会有许多新书加入.为了更方便的管理图书(以便于帮助想要借书的客人快速查找他们是否有他们所需要的书),我们需要设计一个图书查找系统. ...

  9. 【Python算法】哈希存储、哈希表、散列表原理

    哈希表的定义: 哈希存储的基本思想是以关键字Key为自变量,通过一定的函数关系(散列函数或哈希函数),计算出对应的函数值(哈希地址),以这个值作为数据元素的地址,并将数据元素存入到相应地址的存储单元中 ...

随机推荐

  1. Eclipse闪退无法打开的解决方法

    使用Eclipse过程中但是有时会出现打不开闪退的情况,这是为什么呢,遇到这种情况怎么解决.东坡小编通过查找资料,发现如下方法可以解决eclipse打不开闪退,具体操作如下: Eclipse打不开闪退 ...

  2. ASP.NET MVC 4 的JS/CSS打包压缩功能-------过滤文件

    今天在使用MVC4打包压缩功能@Scripts.Render("~/bundles/jquery") 的时候产生了一些疑惑,问什么在App_Start文件夹下BundleConfi ...

  3. 自动去除nil的NSDictionary和NSArray构造方法

    http://www.jianshu.com/p/a1e8d8d579c7 极分享 http://www.finalshares.com/

  4. CSS样式案例(2)-制作一个简单的登录界面

    首先来张完工的效果图. 一.html文件如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  5. 大数据之pig安装

    大数据之pig安装 1.下载 pig download 2. 解压安装 mapreduce模式安装: 1:设置HADOOP_HOME,如果pig所在节点不是集群中的节点,那就需要把集群中使用的hado ...

  6. 【C语言入门教程】5.4 递归

    递归函数 是能够直接或通过另一个函数间接调用自身的函数,调用自身的方法称为递归调用.递归调用的本质是使用同一算法将复杂的问题不断化简,直到该问题解决. 例如求斐波那契数列的某一项算法适用于递归函数实现 ...

  7. 虚拟机安装Ubuntu三种网络模式

    VMWare提供三种工作模式桥接(bridge).NAT(网络地址转换)和host-only(主机模式). NAT(网络地址转换) 在NAT模式下,虚拟系统需要借助NAT(网络地址转换)功能,通过宿主 ...

  8. Ubuntu编译PHP7问题

    安装编译依赖 sudo apt-get -y install build-essential git autoconf sudo apt-get build-dep php5 sudo apt-get ...

  9. Redis学习笔记九:独立功能之慢查询日志

    Redis 的慢查询日志用于记录执行时间超过给定时长的命令请求,用户可以通过这个功能产生的日志来监视和优化查询速度. 服务器配置有两个相关选项: slowlog-log-slower-than 选项指 ...

  10. ubuntu 15.04 手动安装nginx 1.9.0

    平时工作也用nginx,不过用的时候都是已经配好的,只要简单改改参数就可以了.今天在自己的电脑上安装的时候发现没有想象的那么顺利. 纸上得来终觉浅,绝知此事要躬行. 正题: 1.到nginx下载页面获 ...