题目信息

1078. Hashing (25)

时间限制100 ms

内存限制65536 kB

代码长度限制16000 B

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 -

解题思路

题目默认你已掌握平方二次探測,有多坑逼。,,,

AC代码

#include <cstdio>
#include <cmath>
bool a[20005];
bool prime(int a){
if (1 == a) return false;
if (2 == a || 3 == a) return true;
int t = (int)sqrt(a);
for (int i = 2; i <= t; ++i){
if (a%i == 0){
return false;
}
}
return true;
}
int main()
{
int ms, n, ts, t, i, j;
scanf("%d%d", &ms, &n);
ts = ms;
while (!prime(ts)){
++ts;
}
for (i = 0; i < n; ++i){
scanf("%d", &t);
for (j = 0; j < n; ++j){
if (!a[(t + j*j) % ts]){
a[(t + j*j) % ts] = true;
printf("%s%d", i ? " ":"", (t + j*j) % ts);
break;
}
}
int loc = t % ts;
if (j == n){
printf("%s-", i ? " ":"");
}
}
return 0;
}

1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise的更多相关文章

  1. 1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise

    题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chine ...

  2. 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise

    题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...

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

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

  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. PAT (Advanced Level) Practise - 1093. Count PAT's (25)

    http://www.patest.cn/contests/pat-a-practise/1093 The string APPAPT contains two PAT's as substrings ...

  6. PAT (Advanced Level) Practise - 1094. The Largest Generation (25)

    http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...

  7. 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...

  8. PAT (Advanced Level) Practise - 1097. Deduplication on a Linked List (25)

    http://www.patest.cn/contests/pat-a-practise/1097 Given a singly linked list L with integer keys, yo ...

  9. 1016. Phone Bills (25)——PAT (Advanced Level) Practise

    题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A l ...

随机推荐

  1. Verilog 加法器和减法器(6)

    为了减小行波进位加法器中进位传播延迟的影响,可以尝试在每一级中快速计算进位,如果能在较短时间完成计算,则可以提高加法器性能. 我们可以进行如下的推导: 设 gi=xi&yi, pi = xi ...

  2. 在PC上像普通winform程序调试WINCE程序

    在PC上像普通winform程序调试WINCE程序 步骤: 1. 在VS2008中到 工具→选项→设备工具→设备,选择对应的平台,另存为新的名称,如CEDesktopRun,关闭VS2008.(如果不 ...

  3. [leetcode]Text Justification @ Python

    原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...

  4. 杨晓峰-Java核心技术-9 HashMap Hashtable TreeMap MD

    目录 第9讲 | 对比Hashtable.HashMap.TreeMap有什么不同? 典型回答 考点分析 知识扩展 Map 整体结构 有序 Map HashMap 源码分析 容量.负载因子和树化 精选 ...

  5. 操作系统重点双语阅读 - 上下文切换 Context Switch

    The context is represented in the PCB of the process. It includes the value of the CPU registers, th ...

  6. 【转】字符编码笔记:ASCII,Unicode 和 UTF-8

    原文:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html https://www.key-shortcut.com/ ...

  7. OnBecameVisible和OnBecameInvisible ,OnWillRenderObject

    OnBecameVisible 和 OnBecameInvisible ,OnWillRenderObject 只有在所挂物体(不包括子物体)有render才有效 //可见 private void ...

  8. java interface 默认值

    /* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or th ...

  9. Unity3D 学习 创建简单的按钮、相应事件

    选择file -->new project 然后保存到相应的地方 下面是这个刚创建的工程效果图. 然后创建一个C# Script ||定位到最左下角找到  assets --> creat ...

  10. js 判断l对象类型

    var obj = {}; obj.isObject = function (obj) { return Object.prototype.toString.call(obj) == '[object ...