Description

使用线性探测法(Linear Probing)可以解决哈希中的冲突问题,其基本思想是:设哈希函数为h(key) = d, 并且假定哈希的存储结构是循环数组, 则当冲突发生时, 继续探测d+1, d+2…, 直到冲突得到解决.

例如, 现有关键码集为 {47,7,29,11,16,92,22,8,3},

设:哈希表表长为m=11;哈希函数为Hash(key)=key mod 11;采用线性探测法处理冲突。建哈希表如下:

0

1

2

3

4

5

6

7

8

9

10

11

22

 

47

92

16

3

7

29

8

 

现在给定哈希函数为Hash(key)= key mod m,要求按照上述规则, 使用线性探测法处理冲突.要求建立起相应哈希表,并按要求打印。

Input

仅有一个测试用例, 第1行为整数n与m(1 <= n, m <= 10000), n代表key的总数, m代表哈希表的长度, 并且令哈希函数为: Hash(key) = key mod m.

接下来n行,每行一个整数,代表一个key。Key与key两两不相同 ( 0 <= key <= 10, 000)。

Output

输出建立好的hash表,比如下表

0

1

2

3

4

5

6

7

8

9

10

11

22

 

47

92

16

3

7

29

8

 

应输出

0#11

1#22

2#NULL

3#47

4#92

5#16

6#3

7#7

8#29

9#8

10#NULL

想法:要注意的地方就是循环数组那里。。。。其他的就很基本了。。

 #include <iostream>

 using namespace std;

 int main(int argc, char const *argv[])
{
int n, m, modNum, temp;
int* hash;
int * num; cin >> n >> m;
hash = new int[m];
num = new int[n + ]; for (int i = ; i != m; i++)
hash[i] = -; for (int i = ; i != n; i++) {
cin >> temp;
int modNum = temp % m;
if (hash[modNum] == -) {
hash[modNum] = temp;
} else {
int j = modNum + ;
j %= m;//循环数组
for (;j != m; j++) {
if (hash[j] == -) {
hash[j] = temp;
break;
}
j %= m;
}
}
} for (int i = ; i != m; i++) {
if (hash[i] != -)
cout << i << "#" << hash[i] << endl;
else
cout << i << "#NULL" << endl;
}
delete []hash;
return ;
}

sicily 4699. 简单哈希的更多相关文章

  1. sicily 1004. 简单哈希

    Description 使用线性探测法(Linear Probing)可以解决哈希中的冲突问题,其基本思想是:设哈希函数为h(key) = d, 并且假定哈希的存储结构是循环数组, 则当冲突发生时,  ...

  2. POJ 3349 Snowflake Snow Snowflakes(简单哈希)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 39324   Accep ...

  3. POJ:2528(Mayor's posters)离散化成段更新+简单哈希

    http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that the ca ...

  4. C语言-简单哈希表(hash table)

    腾讯三面的时候,叫我写了个哈希表,当时紧张没写好···结果跪了··· 回来后粪发涂墙,赶紧写了一个! 什么都不说了···先让我到厕所里面哭一会··· %>_<% 果然现场发挥,以及基础扎实 ...

  5. [SOJ] 简单哈希

    Description 使用链地址法(又称拉链法)可以解决Hash中的冲突问题.其基本思想是:将具有相同哈希地址的记录链成一个单链表,m个哈希地址就设m个单链表,然后用一个数组将m个单链表的表头指针存 ...

  6. 【bzoj3555】[Ctsc2014]企鹅QQ 简单哈希

    传送门 题目分析 题意即求有多少对字符串只相差一个字符,枚举删除每个字符后的哈希, 看有多少相等即可. 比如有如下字符串:$Sd123$,其中S部分的哈希值为H,删除的是d,则原字符串的哈希值为$$( ...

  7. 元组/字典/集合内置方法+简单哈希表(day07整理)

    目录 二十三.元组内置方法 二十四.字典数据类型 二十五 集合内置方法 二十五.数据类型总结 二十六.深浅拷贝 补充:散列表(哈希表) 二十三.元组内置方法 什么是元组:只可取,不可更改的列表 作用: ...

  8. Sicily 1151: 简单的马周游问题(DFS)

    这道题嘛,直接使用DFS搜索,然后莫名其妙地AC了.后来看了题解,说是move的顺序不同的话可能会导致超时,这时便需要剪枝,真是有趣.原来自己是误打误撞AC了,hhh.题解还有另一种解法是先把一条完整 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. Linux相关——记一些ubuntu相关快捷键&操作(持续更新)

    (有一些是windows通用的...放上来凑字数...) 1, ctrl + alt +  t.调出终端,这个没什么好解释的. 2, win + s.可以快速查看打开的窗口,并进行切换 3,win + ...

  2. BZOJ4698 & 洛谷2463:[SDOI2008]Sandy的卡片——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=4698 https://www.luogu.org/problemnew/show/P2463#sub ...

  3. HDU5696:区间的价值——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=5696 题面是中文的我就不粘贴过来了…… ———————————————————————— 这题垃圾题!!神tm卡 ...

  4. [Leetcode] search insert position 寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. 非代码抽取的dex加固脱壳

    常见的非代码抽取的dex加固,可以通过修改或者hook源码中的dex解析函数拿到目标dex完成脱壳.该dex解析函数为DexFile* dexFileParse(const u1* data, siz ...

  6. Change the IPTables log file

    http://www.networkinghowtos.com/howto/change-the-iptables-log-file/     An important aspect of any f ...

  7. JS判断当前DOM树是否加载完毕

    /** * @function Monitor whether the document tree is loaded. * @param fn */function domReady(fn) { i ...

  8. STL之四:list用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8520891 list容器介绍 相对于vector容器的连续线性空间,list是一个双 ...

  9. DOM案例五星评分控件

    模仿网页上评分的五个五星. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...

  10. DOM动态增加控件

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...