1078. Hashing (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 -

思路:主要是平方探测的定义。若H(key)%size的位置已经被占用,接下来要查询的那些位置是(H(key)+k*k)%mod  其中k>=0&&k<size。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 11000+5
#define MSIZE 11000+5
int is_prime[MSIZE];
int prime[N_MAX];
int sz, m;
int a[N_MAX];
int pos[N_MAX];
int vis[N_MAX];
int seive(int n) {
int p = ;
fill(is_prime, is_prime + n, );
is_prime[] = is_prime[] = ;
for (int i = ; i <= n; i++) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = i*i; j <= n; j += i) {
is_prime[j] = ;
}
}
}
return p;
} int main() {
int num = seive(MSIZE);
while (cin >> sz >> m) {
for (int i = ; i < m; i++) scanf("%d", &a[i]);
if (!is_prime[sz])
for (int i = ; i < num; i++) {
if (sz < prime[i]) {
sz = prime[i];
break;
}
} for (int i = ; i < m; i++) {
int Pos = a[i],tmp=Pos;
bool flag = ;
for (int k = ; k < sz;k++) {
Pos =(tmp+ k*k)%sz;
if (vis[Pos] == ) {
vis[Pos] = ;
pos[i] = Pos;
flag = true;
break;
}
}
if (!flag)pos[i] = -;
}
for (int i = ; i < m; i++) {
if (pos[i] != -)cout << pos[i];
else cout << "-";
printf("%c", i + == m ? '\n' : ' ');
}
}
return ;
}

pat 甲级 1078. Hashing (25)的更多相关文章

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

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

  2. PAT甲级1078 Hashing【hash】

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

  3. PAT 甲级 1078 Hashing

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

  4. PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]

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

  5. 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise

    题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...

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

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

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

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

  8. PAT甲题题解-1078. Hashing (25)-hash散列

    二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...

  9. PAT (Advanced Level) 1078. Hashing (25)

    二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...

随机推荐

  1. mysql四:数据操作

    一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...

  2. Python 正则表达式 匹配次数

    管道可以匹配多个正则表达式中的一个 >>> >>> m=re.search(r'Batman|Tina Fey','Batman and Tina Fey')> ...

  3. VirtualBox下vim无法正常使用问题解决

    由原来的使用VMware转到使用Virtual Box,发现其vim编辑器不是特别好用,需要进行一下更改设置: 1.使用命令删除vim,sudo apt-get remove vim-common 2 ...

  4. python3 练习题100例 (十二)

    题目十二:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153 ...

  5. f触发器、存储过程

    drop trigger trig_insert--删除触发器

  6. ZOJ 3231 Apple Transportation 树DP

    一.前言 红书上面推荐的题目,在138页,提到了关键部分的题解,但是实际上他没提到的还有若干不太好实现的地方.尤其是在这道题是大家都拿网络流玩弄的大背景下,这个代码打不出来就相当的揪心了..最后在牛客 ...

  7. C#+VisionPro连接相机获取图像的两种方式

    两种比较常用的方式. C#直接连接相机获取图像(GIGE) 在获取图像前,需要先创建一个相机对象,再使用这个相机对象的Acquire方法拍摄照片. ICogAcqFifo macqfifo;//定义相 ...

  8. Linux磁盘与文件管理系统

    基本上Linux的正统文件系统为Ext2,该文件系统内的信息主要有: superblock:记录此filesystem的整体信息,包括inode/block的总量,使用量,剩余量,以及文件系统的格式与 ...

  9. day 17 jQuery

    什么是jQuery? 可以把它认为是python中的模块,导入就可以使用模块中的功能. jQuery 的版本: 1.xx 系列 2.xx 系列 3.xx 系列 最常用的为1 系列,1系列最新版为1.1 ...

  10. day38--MySQL基础二

    1.数据库连表 1.1, 一对多 使用外键做约束.注意:外键列的数据类型要一致. 命令的方式创建外键CREATE table part1( nid int not null auto_incremen ...