PAT-1078 Hashing (散列表 二次探测法)
1078. Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key % TSize" where TSize is the maximum
size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers,
respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it
is impossible to insert the number, print "-" instead.
Sample Input:
4 4
10 6 4 15
Sample Output:
0 1 4 -
题目大意:此题给出散列表的大小msize以及要插入的数据n个,要求顺序输出每个数据在散列表中的索引。
主要思想:1.如果给出的msize不是素数,则要找到大于该值的最小素数;
2.用二次探测法(quadratic probing)解决碰撞,而且只考虑正增长;
#include <iostream>
using namespace std;
bool marked[10010]; //注意9973的下一个素数是10007
//判断是否是素数的函数
bool is_prime(int x) {
if (x < 2) return false;
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
} int main(void) {
int msize, n, num, i; cin >> msize >> n;
for (i = msize; ; i++) {
if (is_prime(i)) {
msize = i;
break;
}
}
for (i = 0; i < n; i++) {
cin >> num;
int h = num % msize; //初始散列位置
int hash = h;
bool flag = false; //是否散列成功
for (int k = 0; k < msize; k++) {
hash = (h + k * k) % msize; //二次探测法
if (!marked[hash]) { //成功找到位置
marked[hash] = true;
flag = true;
break;
}
}
if (flag) cout << hash;
else cout << "-";
if (i != n-1) cout << " ";
else cout << endl;
} return 0;
}
PAT-1078 Hashing (散列表 二次探测法)的更多相关文章
- PAT 1078 Hashing[一般][二次探查法]
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
- PAT 1145 1078| hashing哈希表 平方探测法
pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...
- Linux散列表(二)——宏
散列表宏承接了双向链表宏的风范,好使好用!务必区分“结点”和“元素”!双链表宏博文中已经提及,这里不赘述! 1.获取元素(结构体)基址 #define hlist_entry(ptr, type, m ...
- PAT 1078. Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)
题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- Java数据结构与算法解析(十二)——散列表
散列表概述 散列表就是一种以 键-值(key-indexed) 存储数据的结构,我们只要输入待查找的值即key,即可查找到其对应的值. 散列表的思路很简单,如果所有的键都是整数,那么就可以使用一个简单 ...
- 数据结构(四十二)散列表查找(Hash Table)
一.散列表查找的基础知识 1.散列表查找的定义 散列技术是在记录的存储位置和它的关键字之间建立一个确定的对应关系f,使得每个关键字key对应一个存储位置f(key).查找时,根据这个确定的对应关系找到 ...
- [MIT6.006] 8. Hashing with Chaining 散列表
一.字典 在之前课里,如果我们要实现插入,删除和查找,使用树结构,最好的时间复杂度是AVL下的Ο(log2n),使用线性结构,最好的复杂度为基数排序Ο(n).但如果使用字典数据类型去做,时间复杂度可为 ...
随机推荐
- Android 5.0系统默认颜色
伴随着 Android5.0 的发布也更新了support-v7-appcompat 到 V21,其中增加了 ToolBar.recyclerview.cardview 等控件. Android5.0 ...
- 自建Git服务器 - 创建属于你自己的代码仓库
最近有线上朋友私信问我怎么搭建个人博客,也有咨询我个人项目的代码是如何保管的,还有一个朋友问我买了服务器玩了一段时间,等新鲜感过了就不知道做什么了. 关于这些问题并没有一个标准答案,每个人都有自己的使 ...
- 【集群实战】NFS网络文件共享服务2-mount挂载(参数,优化)
1. NFS客户端挂载深入 1.1 NFS客户端挂载参数说明 在NFS服务端,可以通过cat /var/lib/nfs/etab查看NFS服务器端配置参数的细节. 在NFS客户端,可以通过cat /p ...
- CSS样式3
1.positon:fixed 可以实现网页浏览器上的返回顶部的功能. positon:fixed 表示将当前div块固定在页面的某一个位置(默认为左上角). <!DOCTYPE html> ...
- 使用JXL.jar实现JAVA对EXCEL的读写操作
简介: jxl.jar是通过java操作excel表格的工具类库 jxl操作excel包括对象Workbook(工作簿),Sheet(工作表) ,Cell(单元格). 一个excel就对应一个Work ...
- Codeforce 322E Ciel the Commander (点分治)
E. Ciel the Commander Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, ...
- 自定义Element父子不关联的穿梭树
Element自身是有一个Transfer穿梭框组件的,这个组件是穿梭框结合checkbox复选框来实现的,功能比较单一,自己想实现这个功能也是很简单的,只是在项目开发中,项目排期紧,没有闲功夫来实现 ...
- 基于tp3开发的cms-前台代码执行
内核 thinkphp PbootCMS-V1.2.1 ├─ apps 应用程序 │ ├─ admin 后台模块 │ ├─ api api模块 │ ├ ...
- Oracle的pl/sql变量类型
pl/sql定义 sql是结构化查询语言.sql是不是一个编程语言?编程语言一般都能够声明变量,写条件判断,循环.sql不具备这些特征,所有sql不是一门编程语言.我们在实际的开发中,有这种需要,把s ...
- FOC 电流采样方案对比(单电阻/双电阻/三电阻)
如果本文帮到了你,帮忙点个赞: 如果本文帮到了你,帮忙点个赞: 如果本文帮到了你,帮忙点个赞: 创作不易 谢谢支持 文章目录 1 电流采样的作用 2 硬件架构 3 采样关键 4 采样方案 5 三电阻采 ...