Cuckoo for Hashing_双哈希表
问题 B: Cuckoo for Hashing
时间限制: 1 Sec 内存限制: 64 MB
提交: 24 解决: 12
[提交][状态][讨论版]
题目描述
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.)
输入
输出
样例输入
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_双哈希表的更多相关文章
- Redis源码研究:哈希表 - 蕫的博客
[http://dongxicheng.org/nosql/redis-code-hashtable/] 1. Redis中的哈希表 前面提到Redis是个key/value存储系统,学过数据结构的人 ...
- 漫谈Linux内核哈希表(1)
关于哈希表,在内核里设计两个很重要的数据结构: 哈希链表节点: 点击(此处)折叠或打开 .x [include/linux/types.h]*/ struct hlist_node { stru ...
- Java基础知识强化之集合框架笔记75:哈希表
1. 哈希表数据结构(数组): 2. 哈希表确定元素是否相同: (1)判断的是两个元素的哈希值是否相同 如果相同,再判断两个对象内容是否相同 (2)判断哈希值相 ...
- perl5 第九章 关联数组/哈希表
第九章 关联数组/哈希表 by flamephoenix 一.数组变量的限制二.定义三.访问关联数组的元素四.增加元素五.创建关联数组六.从数组变量复制到关联数组七.元素的增删八.列出数组的索引和值九 ...
- 开地址哈希表(Hash Table)的接口定义与实现分析
开地址哈希函数的接口定义 基本的操作包括:初始化开地址哈希表.销毁开地址哈希表.插入元素.删除元素.查找元素.获取元素个数. 各种操作的定义如下: ohtbl_init int ohtbl_init ...
- 开地址哈希表(Hash Table)的原理描述与冲突解决
在开地址哈希表中,元素存放在表本身中.这对于某些依赖固定大小表的应用来说非常有用.因为不像链式哈希表在每个槽位上有一个"桶"来存储冲突的元素,所以开地址哈希表需要通过另一种方法来解 ...
- Hash之哈希表的详解
Hash算法 Hash算法的原理; 决绝冲突的办法是: 线性探查法; 双散列函数法; 拉链法处理碰撞; 哈希原理及实现; 哈希表-Hash table, 也叫散列表;
- 图书管理(Loj0034)+浅谈哈希表
图书管理 题目描述 图书管理是一件十分繁杂的工作,在一个图书馆中每天都会有许多新书加入.为了更方便的管理图书(以便于帮助想要借书的客人快速查找他们是否有他们所需要的书),我们需要设计一个图书查找系统. ...
- 【Python算法】哈希存储、哈希表、散列表原理
哈希表的定义: 哈希存储的基本思想是以关键字Key为自变量,通过一定的函数关系(散列函数或哈希函数),计算出对应的函数值(哈希地址),以这个值作为数据元素的地址,并将数据元素存入到相应地址的存储单元中 ...
随机推荐
- sql字段中逗号分隔字符串的判断
例如,数据表t1中有一个字段PlayTheme存放的数值类似如下: 第一行:1,2,12 第二行:22,222,2222 第三行:1,2 第四行:2,12 第五行:2 如果你想取出PlayTheme字 ...
- VTK初学一,b_PolyVertex多个图形点的绘制
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...
- 关于Tchar
因为C++支持两种字符串,即常规的ANSI编码(使用""包裹)和Unicode编码(使用L""包裹),这样对应的就有了两套字符串处理函数,比如:strlen和w ...
- wordpress不用插件实现Pagenavi页面导航功能
Pagenavi 是一个很好的功能,现在 WordPress 博客一般都是使用 WP-Pagenavi 插件来实现,但是如果插件一多的话整个wordpress效率就降低了,我们力求用尽量少的插件来实现 ...
- 在lua脚本中使用我们自定义的精灵类
首先创建cocos2dx-lua项目,然后在项目中添加我们的自定义精灵类:这里Himi类名为:HSprite // // HSprite.h // cocos2dx_lua_tests_by_Himi ...
- SCP命令
\ svn 删除所有的 .svn文件 find . -name .svn -type d -exec rm -fr {} \; linux之cp/scp命令+scp命令详解 名称:cp 使用权限: ...
- 通过Android录音进行简单音频分析
Android录音有MediaRecorder和AudioRecord两种方式,前者使用方便,可以直接生成录音文件,但是录音格式为aac和amr等等,都经过压缩处理,不方便进行音频分析. 而用Audi ...
- httpd服务访问控制
客户机地址限制 通过配置Order.Deny from.Allow from 来限制客户机 allow.deny :先"允许"后"拒绝" ,默认拒绝所有为明确的 ...
- [codevs1155][KOJ0558][COJ0178][NOIP2006]金明的预算方案
[codevs1155][KOJ0558][COJ0178][NOIP2006]金明的预算方案 试题描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴 ...
- u-boot 流程分析
u-boot 介绍: 对于计算机来说 , 从一开始上机通电是无法直接启动操作系统的 , 这中间需要一个引导过程 , 嵌入式Linux系统同样离不开引导程序 , 这个启动程序就叫启动加载程序(Boot ...