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 ( 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 (≤) and N (≤) 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 -

通过将给定元素值对表长的余数作为在哈希表中的插入位置,如果出现冲突,采用平方探查法解决。平方探查法的具体过程是,假设给定元素值为a,表长为M,插入位置为a%M,假设a%M位置已有元素,即发生冲突,则查找

(a+1^2)%M,(a-1^2)%M,(a+2^2)%M,(a-2^2)%M,⋯⋯,(a+M^2)%M,(a-M^2)%M直至查找到一个可进行插入的位置,否则当查找到(a+M^2)%M,(a-M^2)%M仍然不能插入则该元素插入失败。

 #include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int M, N;
bool isPrime(int num)
{
if (num <= )
return num > ;
if (num % != && num % != )
return false;
int s = (int)sqrt(num);
for (int i = ; i <= s; ++i)
if (num % i == )
return false;
return true;
}
int main()
{
cin >> M >> N;
while (!isPrime(M++));//找到素数
M--;
vector<int>table(M, );
int num, index;
for (int i = ; i < N; ++i)
{
cin >> num;
index = num % M;
if (table[index] > )//存在冲突
{
for (int i = ; i <= M; ++i)
{
index = (num + i * i) % M;
if (table[index] == )
break;
}
}
if(table[index] == )//不存在冲突
{
table[index]++;
cout << index;
}
else
cout << "-";
if (i < N - )
cout << " ";
}
return ;
}

PAT甲级——A1078 Hashing的更多相关文章

  1. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  2. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  3. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  4. PAT甲级1078 Hashing【hash】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 题意: 给定哈希表的大小和n个数,使用 ...

  5. PAT 甲级 1145 Hashing - Average Search Time

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...

  6. PAT 甲级 1078 Hashing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...

  7. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT甲级题分类汇编——杂项

    本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...

  9. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

随机推荐

  1. Linux(Centos7)安装ngnix服务器

    Ngnix服务器是一款优秀的静态页服务器软件和反向代理服务器软件 目前,centos安装ngnix可以yum安装也可以下载安装,我们为了扩展方便,选择下载安装.yum一键安装没什么好说的. 一.安装编 ...

  2. Linux服务器下对Oracle数据库expdp(导出)和impdp(导入)

    紧接上篇文章,Oracle数据库架构已经创建完成,我的需求是:将老服务器上的数据库迁移到新的数据库上. 这就用到impdp(导入)操作. 要想实现对新数据库的impdp(导入)工作, 首先需要从老的数 ...

  3. java Twain 直接打印/界面打印

    这两天,在搞归档系统.需要用到Twain协议来驱动扫描仪. 找了两天,java的twain操作资料真的不多.而且我还是要找直接打印的功能. 后来只能静下心来看类库和源码.最后搞定他. 打印方式分为3种 ...

  4. 【学术篇】一些水的不行的dp

    最近做了几道非常水非常水的dp...... 之后刷的一些水dp也会写在这里...... 此篇题目难度不递增!!! Emmmm....... 1.luogu1043数字游戏 以前看过这个题几遍,没做这个 ...

  5. Android Studio 配置快速生成模板代码

    前言 Android studio 有提供快速生成模板代码的功能,其实这个功能也可以自定义配置.此篇博客将讲解如何使用此功能 进入Settings 选择 Editor > Live Templa ...

  6. Idea中创建maven骨架的命令

    如下:通过命令化在Idea中创建骨架成功后,以后项目直接引用导入骨架直接在依赖框架上面进行相关模块开发: 1.mvn archetype:create-from-project 2.mvn clean ...

  7. python非对称加密模块rsa

    一.代码 # 导入rsa库 import rsa.common class RSA(object): def __init__(self): self.key= rsa.newkeys(256) se ...

  8. FHS 文件层次标准

    FHS:文件层次标准 操作系统自身运行使用的 /bin: 存放可执行的二进制程序,管理员和普通用户都可以使用 /sbin:管理员才能执行的命令 运行正常功能的程序存放位置 /usr/bin /usr/ ...

  9. MDK(KEIL)使用Astyle格式化代码

    关于Astyle Astyle 的全称是Artistic Style的简称,是一个开源的源代码格式化工具,可以对C,C++,C#以及Java等编程语言的源代码进行缩进.格式化.美化. Home Pag ...

  10. elasticsearch的索引清理

    curl -XDELETE 'http://127.0.0.1:9200/winlogbeat-6.0.0-2017.07.*' 脚本加api删除(推荐) cat es-index-clear.sh ...