HNU 13064 Cuckoo for Hashing解题报告 North America - East Central 2013
题目大意:使用两个哈希表来解决哈希冲突的问题。假如现在有两个哈希表分别为:H1,H2 ,大小分别为:n1,n2;现有一数据X需要插入,其插入方法为:
1、计算index1 = X MOD N1, 若哈希表H1的index1位置没有保存数据,则直接将X保存到H1得index1;否则,若哈希表H1的index1位置已经保存有数据X1,则将原来已保存的数据X1进行缓存,然后将X插入H1的index1的位置。
2、将上一步缓存的X1插入到哈希表H2,首先计算index2=X1 MOD N2,若H2的index2没有保存数据,则直接将X1保存至index2,;否则,缓存原来在H2中index2的数据X2,然后将X1保存到H2的index2中。
3、将上一步得X2重新插入到哈希表H1中,依次类推。
样例输入输出
| Sample Input |
5 7 4 |
| Sample Output |
Case 1: |
解题思路:
1、创建两个新的空哈希表,对于每个需要插入的数据分别进行处理。
2、对于每一个需要插入的数据,根据两个哈希表以上的性质,进行插入。
代码如下:
<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int t1[1002],t2[1002];
int flag;
/*flag==0, the operation in the table1
flag==1, the operation in the table2
when the collision occur, it will
*/
void insert(int n1, int n2, int value)
{
int index, hel, tmp;
switch (flag)
{
case 0: //in the table1
hel = value%n1;
if(t1[hel] != 0)
{
flag = 1;
tmp = t1[hel];
t1[hel] = value;
insert(n1, n2, tmp);
}
else {
t1[hel] = value;
}
break;
case 1: //in the table2;
hel = value%n2;
if(t2[hel] != 0)
{
flag=0;
tmp = t2[hel];
t2[hel] = value;
insert(n1, n2, tmp);
}
else{
t2[hel] = value;
}
break;
}
}
int main()
{
int n1,n2,m,count;
int i,value,f; count = 0;
while(scanf("%d%d%d",&n1,&n2,&m)==3)
{
if(!n1 && !n2 && !m)
break;
memset(t1,0,sizeof(t1));
memset(t2,0,sizeof(t2));
for(i=0; i<m; i++)
{
scanf("%d",&value);
flag = 0;
insert(n1, n2, value); }
printf("Case %d:\n",++count);
f=0;
for(i=0; i<n1; i++)
{
if(t1[i] != 0)
{
if(0 == f)
{
printf("Table 1\n");
f = 1;
}
printf("%d:%d\n",i,t1[i]);
}
}
f=0;
for(i=0; i<n2; i++)
if(t2[i] != 0)
{
if(0 == f)
{
printf("Table 2\n");
f=1;
}
printf("%d:%d\n",i,t2[i]);
}
}
return 0;
}</span>
HNU 13064 Cuckoo for Hashing解题报告 North America - East Central 2013的更多相关文章
- 组队练习赛(Regionals 2012, North America - East Central NA)
A.Babs' Box Boutique 给定n个盒子,每个盒子都有长宽高(任意两个盒子长宽高不完全相同),现在选盒子的任意两面,要求x1 <= x2 && y1 <= y ...
- 130825组队赛-Regionals 2012, North America - East Central NA
A.Babs' Box Boutique 一道简单的dfs搜索题,需要两两比较,然后搜到底,得到最大值就行了.比赛时队友写的,我只负责debug..赛后自己写的.. #include<iostr ...
- HNU 13074 Goldbach’s Conjecture 解题报告
题目大意:输入一个偶数(x<32000),输出这个偶数是由哪两个素数相加所得. 比如:一个偶数26,它可以是3+23,7+19,13+13,这些素数相加所得. 输入输出样例: Sample In ...
- HNU 13081 Even Up Solitaire解题报告
题目大意:给定一个数组,若相邻的两个数之和为偶数,则将此两个数移除,通过这种方法将满足条件得数移除后数组还剩多少个数. 此题太水,不做解释.直接代码之: #include <stdio.h> ...
- USACO Section2.1 The Castle 解题报告
castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- CH Round #56 - 国庆节欢乐赛解题报告
最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...
- 二模13day1解题报告
二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...
- BZOJ 1051 最受欢迎的牛 解题报告
题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4438 Solved: 2353[S ...
- 习题:codevs 2822 爱在心中 解题报告
这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...
随机推荐
- Android手机fastboot刷机命令
先进入fastboot文件所在目录 连接硬件命令 fastboot devices 删除recover.boot,system同理 Fastboot erase recovery 重刷,boot,sy ...
- Android命令行工具logcat详细用法!
logcat是Android中一个命令行工具,可以用于得到程序的log信息. 见板凳详细说明! 本贴内容来自网络,引用网址为:http://hi.baidu.com/%C9%C1%D2%AB% ...
- 关于jsp页面 title中文乱码问题的解决方法
我知道了 我jsp饮用了html 是我2个页面都写了Title 然后冲突了 就乱码了 分享给大家
- CenOS 用PF_RING优化Snort
0.优化顺序 安装PF_RING的kernel模块 安装PF_RING的用户态库 安装Snort的DAQ 安装PF_RING的pfring-daq-module 安装snort 安装PF_RING-a ...
- Xamarin For Visual Studio 3.0.54.0 完整离线破解版
Xamarin For Visual Studio 3.0.54.0 完整离线破解版 Xamarin For Visual Studio就是原本的Xamarin For Android 以及 Xama ...
- PowerDesigner中几个使用技巧
一.主键自增 二.设置列的约束 三.修改Name和Code一起改变的选项 Tools = > Generator Options=>Dialog -> Name to Code mi ...
- jQuery HighchartsTableHTML表格转Highcharts图表插件
版权申明jQuery HighchartsTable 由 PMSIpilot 创建,中文使用文档由Highcharts中文网发布本文由Theo.红烧鸡翅膀.Mr.Zhang 翻译整理,版权归Highc ...
- [转] M2E插件maven-dependency-plugin问题
转自 : http://blog.csdn.net/cskgnt/article/details/8530526 问题: maven-dependency-plugin (goals "co ...
- iOS多视图传值方式之通知传值(NSNotification;NSNotificationCenter)
iOS传值方式之5:通知传值 第一需要发布的消息,再创建NSNotification通知对象,然后通过NSNotificationCenter通知中心发布消息(NSNotificationCenter ...
- (简单) POJ 2492 A Bug's Life,二分染色。
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...