题目大意:使用两个哈希表来解决哈希冲突的问题。假如现在有两个哈希表分别为: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. Android Studio中有没有类似于Eclipse中的ctrl+2+L的快捷键? \Android Studio快捷键之代码提示

    问:Android Studio中有没有类似于Eclipse中的ctrl+2+L的快捷键? 答:有,as中的快捷键是Ctrl+Alt+V AndroidStudio和Eclipse常用快捷键对比 功能 ...

  2. springmvc json数据

    的 @RequestMapping("/getAllEdu") @ResponseBody public void getAllEdu(HttpServletRequest req ...

  3. Struts2 程序步骤

    1. 新建一个web project, 手动导入包: D:\Java\jar\struts-2.3.24.1\apps\struts2-blank\WEB-INF\lib copy到 WEB-INF/ ...

  4. MyEclipse中提示SpringMVC的XML配置文件出错解决方法

    手动添加schema文件,方法如下: 1,依次选择:windwos->preferences->myeclipse->files and editors->xml->xm ...

  5. Kafka 在行动:7步实现从RDBMS到Hadoop的实时流传输

    原文:https://coyee.com/article/11095-kafka-in-action-7-steps-to-real-time-streaming-from-rdbms-to-hado ...

  6. gcd timer

    //0.创建队列 dispatch_queue_t queue = dispatch_get_global_queue(, ); NSLog(@"%s",__func__); // ...

  7. Spring--AOP--面向切面编程

    AOP: 面向切面编程. 通过动态代理实现. AOP就3条线, 2条线给剪断. 实现:动态代理 如果实现interface的话, 用Proxy, InvocationHandler. 不实现inter ...

  8. 10个带源码的充满活力的Web设计教程

    10个带源码的充满活力的Web设计教程 2013-08-02 16:47 佚名 OSCHINA编译 我要评论(0) 字号:T | T Web设计师必须了解各种各样的Web设计风格,这才能让他或者她在设 ...

  9. CentOS7 开源跳板机(堡垒机) Jumpserver

    开源跳板机(堡垒机)Jumpserver 环境 CentOS 7   x64       关闭 selinux  firewalld jumpserver: 172.24.0.14 testserve ...

  10. (简单) POJ 1562 Oil Deposits,BFS。

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...