题目大意:使用两个哈希表来解决哈希冲突的问题。假如现在有两个哈希表分别为: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
8 18 29 4
6 7 4
8 18 29 4
1000 999 2
1000
2000
0 0 0
Sample Output
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

解题思路:

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的更多相关文章

  1. 组队练习赛(Regionals 2012, North America - East Central NA)

    A.Babs' Box Boutique 给定n个盒子,每个盒子都有长宽高(任意两个盒子长宽高不完全相同),现在选盒子的任意两面,要求x1 <= x2 && y1 <= y ...

  2. 130825组队赛-Regionals 2012, North America - East Central NA

    A.Babs' Box Boutique 一道简单的dfs搜索题,需要两两比较,然后搜到底,得到最大值就行了.比赛时队友写的,我只负责debug..赛后自己写的.. #include<iostr ...

  3. HNU 13074 Goldbach’s Conjecture 解题报告

    题目大意:输入一个偶数(x<32000),输出这个偶数是由哪两个素数相加所得. 比如:一个偶数26,它可以是3+23,7+19,13+13,这些素数相加所得. 输入输出样例: Sample In ...

  4. HNU 13081 Even Up Solitaire解题报告

    题目大意:给定一个数组,若相邻的两个数之和为偶数,则将此两个数移除,通过这种方法将满足条件得数移除后数组还剩多少个数. 此题太水,不做解释.直接代码之: #include <stdio.h> ...

  5. USACO Section2.1 The Castle 解题报告

    castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  6. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  7. 二模13day1解题报告

    二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...

  8. BZOJ 1051 最受欢迎的牛 解题报告

    题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4438  Solved: 2353[S ...

  9. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

随机推荐

  1. mac编译openssl扩展报错 openssl.c:44:10: fatal error: 'openssl/evp.h' file not found

    解决办法 brew link openssl --force 然后 ./configure --with-openssl --with-php-config=/usr/local/php/bin/ph ...

  2. laravel提示Mcrypt PHP extension required

    系统Ubuntu 安装Apache,php后发现laravel报 Mcrypt PHP extension required错误 解决办法: apt-get install php5-mcrypt c ...

  3. sql语句删除由于无主键导致完全重复的数据方法

    sql语句删除由于无主键导致完全重复的数据方法 select distinct * into #Tmp from t_column drop table t_column select * into ...

  4. css一些特别效果设定

    在CSS中,BOX的Padding属性的数值赋予顺序为 padding:10px; 四个内边距都是10pxpadding:5px 10px; 上下5px 左右10pxpadding:5px 10px ...

  5. 在 Android 中调用二进制可执行程序(native executable )

    前几天有需要在java代码中调用二进制程序,就在网上找了些资料,写点东西记录下. Android 也是基于linux 的系统,当然也可以运行二进制的可执行文件.只不过Android 限制了直接的方式只 ...

  6. PAT (Advanced Level) 1077. Kuchiguse (20)

    最长公共后缀.暴力. #include<cstdio> #include<cstring> #include<cmath> #include<vector&g ...

  7. cmd 3389

    1.2000系统 简要说一下如何进行DNS溢出攻击.我用的溢出利用程序是dns.exe,在CMD下运行它可以看到它的使用参数等信息.执行"dns -s IP"命令检测目标IP是否存 ...

  8. WebDriver(Selenium2) 处理可能存在的JS弹出框

    http://uniquepig.iteye.com/blog/1703103 在自动化测试过程中,有些情况下我们会遇到一些潜在的Javascript弹出框.(即某些条件下才会出现,不是固定出现),然 ...

  9. SpringMVC+Spring+hibernate整合及分页

    1. 新建web project 2. 引入jar, 3. 创建包com.tgb.web.controller, 下面创建包(dao,entity,service, config,spring,hib ...

  10. 关于 CentOS 7 里面 普通用户 Ulimit max user processes 值的问题

    最近在对tomcat 的一个 项目进行 压测, 普通用户 启动 tomcat 的时候 压力上去以后就会报 java.lang.OutOfMemoryError 的错误, 这种错误 按道理来说都是 系统 ...