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  (≤10​^4​​)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 -
#include<cstdio>
const int maxn = ; int hashTable[maxn] = {}; bool isPrime(int n)
{
if (n <= ) return false;
for (int i = ; i * i <= n; i++)
{
if (n % i == )
{
return false;
}
}
return true;
} int main()
{
int n,m;
scanf("%d%d", &m, &n); while(!isPrime(m))
{
m++;
} int pos, temp;
for (int i = ; i < n; i++)
{
scanf("%d",&temp);
pos = temp % m;
if (!hashTable[pos])
{
hashTable[pos] = ;
printf("%d", pos);
}
else
{
int step = ;
for (; step <= m; step++)
{
pos = (temp + step * step) % m;
if (!hashTable[pos])
{
hashTable[pos] = ;
printf("%d", pos);
break;
}
}
if (step >= m)
{
printf("-");
}
} if (i < n - )
{
printf(" ");
}
} return ;
}

11-散列2 Hashing (25 分)的更多相关文章

  1. PTA 字符串关键字的散列映射(25 分)

    7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...

  2. PTA 11-散列2 Hashing (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/679 5-17 Hashing   (25分) The task of this pro ...

  3. pat09-散列1. Hashing (25)

    09-散列1. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue The task of ...

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

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

  5. PTA 逆散列问题 (30 分)(贪心)

    题目链接:https://pintia.cn/problem-sets/1107178288721649664/problems/1107178432099737614 题目大意: 给定长度为 N 的 ...

  6. JavaScript数据结构-11.散列

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 5-17 Hashing (25分)

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  8. PAT 列车厢调度   (25分)(栈和容器的简单应用)

    1 ====== <--移动方向 / 3 ===== \ 2 ====== -->移动方向 大家或许在某些数据结构教材上见到过“列车厢调度问题”(当然没见过也不要紧).今天,我们就来实际操 ...

  9. 纯数据结构Java实现(11/11)(散列)

    欢迎访问我的自建博客: CH-YK Blog.

随机推荐

  1. ZYNQ笔记(2):PS端——Hello World !

    PL端使用过后,来到了ZYNQ核心的部分:PS端,现在用Vivado软件对ZYNQ-7000开发板的PS端进行第一个程序设计:Hello World. 一.新建Vivado工程 1.打开Vivado, ...

  2. JSONBuilder的用法

    一.JSONBuilder可以向文件中写入写入json字符串.如下面的例子: 1 public class Test 2 { 3 public static void main(String args ...

  3. chrome(谷歌)浏览器字体发虚解决办法

    chrome(谷歌浏览器)浏览网页时,字体发虚的解决办法: 1.点击chrome里的 “设置” - 外观 - 字体,改为 微软雅黑,该方法测试无效. 2.将系统字体的pingfang字体卸载,完美解决 ...

  4. Python 绘图与可视化 matplotlib text 与transform

    Text 为plots添加文本或者公式,反正就是添加文本了 参考链接:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.text.html#ma ...

  5. windows 提权脚本利用

    本地加载: Import-Module Sherlock.ps1 远程加载: IEX (New-Object System.Net.Webclient).DownloadString('https:/ ...

  6. Cobaltstrike安装

    1.下载JDK http://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html#jd ...

  7. SUCTF 2018——Anonymous(php匿名函数 \x00lambda_)

    题目地址:http://45.76.173.177:23334/ <?php $MY = create_function("","die(`cat flag.php ...

  8. Android 培训准备资料之project与module的区别(1)

    project和module的区别? 现在我们来看看在Android studio中怎样新建一个project (1)file->new->new project. Application ...

  9. MySQL 重要参数 innodb_flush_log_at_trx_commit 和 sync_binlog

    innodb_flush_log_at_trx_commit 主要控制了innodb将log buffer中的数据写入日志文件并flush磁盘的时间点,取值分别为0.1.2三个.该参数控制重做日志写入 ...

  10. 01、Linux基础命令

    linux 一些主要目录的认识: /bin 二进制可执行命令 /boot 存放系统引导文件,如 内核.grub 等 /dev 设备文件 /etc 系统配置目录 /home 普通用户家目录 /lib 系 ...