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语句学习笔记
从外部EXCEl文件导入sqlserver数据库操作命令 reconfigure reconfigure go select * into abc1_1 from OPENROWSET('MICROS ...
- Linux服务器管理: 系统的定时任务crond
cornd 是定时任务的守护进程 这个服务系统是默认启动的 [root@localhost/]#/etc/init.d/crond strat|restart|stop [root@localhos ...
- 深入浅出的javascript的正则表达式学习教程
深入浅出的javascript的正则表达式学习教程 阅读目录 了解正则表达式的方法 了解正则中的普通字符 了解正则中的方括号[]的含义 理解javascript中的元字符 RegExp特殊字符中的需要 ...
- [译]git checkout
git checkout git checkout提供3种不同的功能: checking out文件, checking out commits, checking out branch. check ...
- SQLite初试...
string dataPath = "../../UserData.dbx"; //System.IO.Directory.GetCurrentDirectory() + &quo ...
- JS跳转后台
location.href = ROOT+"?"+VAR_MODULE+"=FaPiao&"+VAR_ACTION+"=dell&id ...
- 跨域攻击xss
要完全防止跨域攻击是很难的,对于有些站点是直接 拦截跨域的访问,在你从本站点跳转到其他站点时提醒,这算是一种手段吧. 而跨域攻击的发起就是在代码(包括html,css,js或是后台代码,数据库数据)里 ...
- js文字滚动
<style type="text/css"> #gongao{width:1000px;height:30px;overflow:hidden;line-hei ...
- [转]CentOS 5.3通过yum升级php到最新版本的方法
来自:www.jasonlitka.com/media 通过测试,方法三可行: 方法三 vim /etc/yum.repos.d/utterramblings.repo 输入 [utterrambli ...
- 淘宝(阿里百川)手机客户端开发日记第十二篇 mysql的认识
我这里用的是wamp,大家可以到网上去下载对应的包,自行安装,对于程序员来讲,安装软件大部分都应该不是问题的,所以我不去将具体安装的方法. wamp安装好后,在我们屏幕的右下角, 这样的图标,我们右键 ...