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 \% TSizeH(key)=key%TSizewhere TSizeTSize 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: MSizeMSize(\le 10^4≤10​4​​) and NN (\le MSize≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then NN 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 "iostream"
#include "cmath"
using namespace std;
#define MAXSIZE 20000 /* 带进去算了下这区间有素数 10006 我也是试着取得0..0.。*/
int nextPrime(int n) {
int i, j;
bool flag = true;
if (n % == )
n++;
if (n == ) /* 判断素数要注意1啊~ 在这里卡了2次- - */
return ;
for (i = n; i < MAXSIZE; i+=) {
int k = sqrt(i);
for (j = ; j<=k ; j++)
if (!(i%j))
break;
if (j > k)
return i;
}
return i;
} int val[];
void find(int a[],int m,int n) {
int i, j;
for (i = ; i < m; i++) {
for (j = ; j < n; j++) {
int pos = (val[i]%n + j*j) % n;
if (a[pos] == val[i])
{
if (i == )
cout << pos;
else
cout << " " << pos;
break;
}
}
if (j == n)
if (i == ) {
cout << "-";
}
else {
cout << " " << "-";
}
}
} void insert(int n,int m,int a[]) {
for (int i = ; i < m; i++) {
cin >> val[i];
for (int j = ; j < n; j++) {
int pos = (val[i]%n + j*j) % n;
if (!a[pos]) {
a[pos] = val[i];
break;
}
}
}
}
int main() {
int n, m;
int a[];
cin >> n >> m;
n = nextPrime(n);
for (int i = ; i < n; i++)
a[i] = ;
insert(n, m, a); /* 映射 */
find(a, m, n);
cout << endl;
return ;
}

5-17 Hashing (25分)的更多相关文章

  1. 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 ...

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

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

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

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

  4. 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)

    题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...

  5. 1078 Hashing (25分)

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

  6. 1078 Hashing (25 分)

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

  7. [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)

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

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

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

  9. L2-001 紧急救援 (25 分)

    L2-001 紧急救援 (25 分)   作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...

随机推荐

  1. 编程添加"作为服务登录”权利(包括例子和API)

    搜索"log on as a service programmatically" https://msdn.microsoft.com/en-us/library/windows/ ...

  2. vmware tools安装程序无法继续,Microsoft Runtime DLL安装程序未能完成安装。的解决方法

    vmware tools安装程序无法继续,Microsoft Runtime DLL安装程序未能完成安装.的解决方法_华英雄_新浪博客 http://blog.sina.com.cn/s/blog_5 ...

  3. swap chain- IDirect3DSwapChain9

    交换链,对应的接口是IDirect3DSwapChain9,是整个Directx 3D中最核心的接口.D3d程序至少包含一个swap chain,在调用CreateDevice时自动创建, 其中的D3 ...

  4. SQL查询性能分析之(not in)、(and not)、()、(!=)性能比较

    SQL查询性能分析之(not in).(and not).().(!=)性能比较 SQL Server Bruce 3年前 (2013-01-08) 3284浏览 0评论 <:article c ...

  5. Apache James搭建内网邮件服务器

    Apache James搭建内网邮件服务器 极客521 | 极客521 2014-08-21 148 阅读 java 大概之前两个礼拜的日子,讨论会介绍了关于了.net内网邮件服务器的搭建.所以自己也 ...

  6. P151、面试题27:二叉搜索树与双向链表

    题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.(本质是中序遍历)二叉树结点的定义如下:struct BinaryTreeNod ...

  7. linux命令之-pstree使用说明

    pstree  shows running processes as a tree. The tree is rooted at either pid or init if pid is omitte ...

  8. ubuntu查看命令

    以非root用户更新系统 sudo: sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,如halt,reboot,su等等.这样不仅减少了root用 ...

  9. [VC6]ONMESSAGE()宏编译时出现"sytax error ;"错误时

    自定义消息时编译出错,经排查,在定义消息的头文件里 #define WM_XXX (WM_USER+1000); 最后多加了一个分号引起. 吐血.

  10. NK 1137: 石子合并问题

     1137: 石子合并问题 Time Limit: 1500 ms    Memory Limit: 10000 kB    Judge type: Multi-cases Total Submit ...