(PAT 1145) Hashing - Average Search Time (哈希表冲突处理)

曾经终败给现在 2022-03-18 06:43 277阅读 0赞

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). 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 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 104. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 105.

Output Specification:

For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted. where X is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.

Sample Input:

  1. 4 5 4
  2. 10 6 4 15 11
  3. 11 4 15 2

Sample Output:

  1. 15 cannot be inserted.
  2. 2.8

解题思路:

该题就是PAT 1078的改版,多要求了一个查找时间

查找思路和插入思路一致,使用二次平方探测法(从0开始) 如果查找的位置为目标值或者不存在数字,则表示找到(没找到),保存查找时间退出循环,然后开始查找下一个数字

这里有一个问题一直没想明白,为什么查找的时候还要多加1???是不是题目出错了???

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <math.h>
  4. using namespace std;
  5. const int MAXS = 100010;
  6. int MSize, N, M;
  7. int AveHashTable[MAXS];
  8. bool isprime(int num) {
  9. if (num <= 1) return false;
  10. for (long long i = 2; i*i <= num; ++i) {
  11. if (num % i == 0) return false;
  12. }
  13. return true;
  14. }
  15. int main() {
  16. fill(AveHashTable, AveHashTable + MAXS, -1);
  17. scanf("%d %d %d", &MSize, &N, &M);
  18. while (!isprime(MSize)) {
  19. MSize++;
  20. }
  21. //insert the key
  22. int number;
  23. for (int i = 0; i < N; ++i) {
  24. scanf("%d", &number);
  25. int cpos = number % MSize;
  26. if (AveHashTable[cpos] == -1) {
  27. AveHashTable[cpos] = number;
  28. }
  29. else { //二次探查法
  30. int newpos,cstep = 1;
  31. while (true) {
  32. newpos = (number + cstep * cstep) % MSize;
  33. if (cstep >= MSize) {
  34. printf("%d cannot be inserted.\n", number);
  35. break;
  36. }
  37. if (AveHashTable[newpos] == -1) {
  38. AveHashTable[newpos] = number;
  39. break;
  40. }
  41. cstep++;
  42. }
  43. }
  44. }
  45. //平均查找时间
  46. double aveTime = 0.0;
  47. int snum;
  48. for (int i = 0; i < M; ++i) {
  49. scanf("%d", &snum);
  50. int sstep = 0,stimes = 0;
  51. while (true) {
  52. int searchPos = (snum + sstep * sstep) % MSize;
  53. if (sstep > MSize) { //为什么要多查找一次???????????
  54. break;
  55. }
  56. //查找一次
  57. stimes++;
  58. if (AveHashTable[searchPos] == snum || AveHashTable[searchPos] == -1) { //不存在数字表示也没有找到
  59. break;
  60. }
  61. sstep++;
  62. }
  63. aveTime += (1.0*stimes);
  64. }
  65. printf("%.1lf\n", aveTime / M);
  66. system("PAUSE");
  67. return 0;
  68. }

发表评论

表情:
评论列表 (有 0 条评论,277人围观)

还没有评论,来说两句吧...

相关阅读

    相关 数据结构 Hash

    一、什么是Hash表 要想知道什么是哈希表,那得先了解哈希函数 哈希函数 对比之前博客讨论的二叉排序树 二叉平衡树 红黑树 B B+树,它们的查找都是先从根节点进行

    相关 处理冲突的方法

    一。哈希函数和哈希冲突的基本概念 > 1.哈希函数: >   哈希法又称散列法、杂凑法以及关键字地址计算法等,相应的表成为哈希表。 >    基本思想: 首先在