问题 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. ASO优化总结(基于网络分享的知识总结归纳)

    如何优化应用标题? 注意关键字的长度,尽量保证每一个关键字小于10个字符.保持快速更新,因为每次更新,你都将有机会删除表现不佳的关键字以 及增添新的关键字.在ASO中使用关键字的正确做法 标题,并非越 ...

  2. Markdown 写作工具选择

    Markdown 写作工具选择 候选产品 参考了少数派网站 markdown 写作工具2015年度盘点 http://sspai.com/32483, Windows 下 Markdown 的编辑工具 ...

  3. [译]angularjs directive design made easy

    原文: http://seanhess.github.io/2013/10/14/angularjs-directive-design.html AngularJS directives很酷 Angu ...

  4. Tomcat 6.0 简介

    本片翻译来自:http://tomcat.apache.org/tomcat-6.0-doc/introduction.html 介绍 无论是开发者还是tomcat管理员在使用前都需要了解一些必要的信 ...

  5. 增值税——基础知识

    一.增值税的概念 增值税是对从事销售货物或者提供加工.修理修配劳务以及从事进出口货物的单位和个人取得的增值额为课税对象征收的一种税. 增值额是指纳税人在生产.经营或劳务活动中所创造的新增价值,即纳税人 ...

  6. 东京区域2012-2014主要消费产品价格参考表——Excel

    声明: 1.本表格数据取自<日本の統計 2016>: 2.本表所有价格单位为人民币,其日元均以当年平均汇率兑换为此人民币价格: 3.其人民币—日元年均汇率数据取自IMF Data Exch ...

  7. PHP封装一个通用好用的文件上传处理类

    封装一个文件上传类完成基本功能如下: 1.可上传多个或单个文件 2.上传成功返回一个或多个文件名 3.上传失败则返回每个失败文件的错误信息 上传类中的基本功能: 1.构造参数,用户可以自定义配置参数, ...

  8. js面试题

    前几天在学习js的时候,碰到了这样一道面试题,要求计算出给你一个随机乱敲的一个字符串,要求在其中找出那个字符出现的次数最多,以及出现的个数. 这你有两种方案,请大家仔细阅读,有可能在你将来的面试中会碰 ...

  9. PHP Socket实现websocket(一)基本函数介绍

    WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex). 一开始的握手需要借助HTTP请求完成. WebSocket是基于TCP来实 ...

  10. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...