题目信息

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代码

  1. #include <cstdio>
  2. #include <cmath>
  3. bool a[20005];
  4. bool prime(int a){
  5. if (1 == a) return false;
  6. if (2 == a || 3 == a) return true;
  7. int t = (int)sqrt(a);
  8. for (int i = 2; i <= t; ++i){
  9. if (a%i == 0){
  10. return false;
  11. }
  12. }
  13. return true;
  14. }
  15. int main()
  16. {
  17. int ms, n, ts, t, i, j;
  18. scanf("%d%d", &ms, &n);
  19. ts = ms;
  20. while (!prime(ts)){
  21. ++ts;
  22. }
  23. for (i = 0; i < n; ++i){
  24. scanf("%d", &t);
  25. for (j = 0; j < n; ++j){
  26. if (!a[(t + j*j) % ts]){
  27. a[(t + j*j) % ts] = true;
  28. printf("%s%d", i ?
  29. " ":"", (t + j*j) % ts);
  30. break;
  31. }
  32. }
  33. int loc = t % ts;
  34. if (j == n){
  35. printf("%s-", i ? " ":"");
  36. }
  37. }
  38. return 0;
  39. }

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. Path Sum leetcode java

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  2. BUG的严重级别分类 BUG状态标准

    英文参考 BUG的严重级别分类 Severity This field describes the impact of a bug. Blocker Blocks development and/or ...

  3. 用Visual C#来清空回收站(1)

    视窗操作系统的回收站是对文件一种保护措施,他主要是作用是不言而喻的.在新的视窗2000系统之中,当我把文件删除到回收站中以后,按动"清空回收站"按钮,想清空回收站,此时往往提示&q ...

  4. Linux:磁盘挂载

    本来虚拟centos的服务器的磁盘分配的就不大,之前只分配了20G的样子,由于最近有装了不少软件,比如nifi压缩版就有1.2G的大小,一下子没有磁盘资源了.今晚就折腾在这事上了. [root@mas ...

  5. 从Linux服务器下载网站文件

    最近公司迁来一个新客户,该客户的网站是别的网络服务商做的,放在linux主机上,因为客户跟之前的网络服务商合作的不愉快 所以就把网站迁到我们公司,经理让我把网站文件和数据库download下来并在我们 ...

  6. linux命令大全网站

    一. linux命令大全网站 http://man.linuxde.net/watch

  7. Eclipse关掉项目SVN的链接

    有时候 svn 会导致 eclipse 反应很慢,可以关掉 svn项目信息展现. 1. 点击项目文件夹,右键出现项目信息 2. 选择team项 3. Disconnect.

  8. hdu Boring count(BestCode round #11)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. [Canvas]计时表/秒表

    欲观看效果请点击下载,然后用浏览器打开index.html查看. 本作 Github地址:https://github.com/horn19782016/StopWatch 图例: 代码: <! ...

  10. C#.NET常见问题(FAQ)-如何判断某个字符是否为汉字

    字符强制转换成int可以判断字符数值大小,在下面所示范围内的就是中文   此外还可以判断是否是数字或者字母,用char.IsLetter和char.IsDigit方法   从先这个范例可以看出,中文也 ...