1145 Hashing - Average Search Time(25 分)
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 10​4​​. 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 10​5 .

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:

4 5 4
10 6 4 15 11
11 4 15 2

Sample Output:

15 cannot be inserted.
2.8

题意:
给定的问题很简单:插入一段不同的正整数序列到hash表中。尝试从表中找到另一组整数序列并输出平均查找时间(找当前键是否在表中的比较次数)。hash函数被定义为H(key)=key%TSize,TSize是hash表的大小。平方探测法(Quadratic probing)被用来解决冲突。
注意表的大小最好是素数(prime)。如果给的size不是素数,你必须重新定义表size为大于使用者给定的size的最小素数。

思路:
1.如果发生冲突则采用平方探测法:
最终位置=(num%size+j*j),j的取值范围是0~size-1
2.如果在j的取值范围内所有的位置都占满了则输出"x cannot be inserted"。
3.考到一个判断素数的方法和一个平方探测方法。

题解:

 #include<cstdlib>
 #include<cstdio>
 #include<vector>
 using namespace std;
 bool isPrime(int num) {
     //给的num可能有<=1的情况
     ) return false;
     //判断素数
     ; i * i <= num; i++) {
         )return false;
     }
     return true;
 }
 int main() {
     int size, n, m;
     scanf("%d %d %d", &size, &n, &m);
     while (!isPrime(size)) {
         size++;
     }
     //vector可以直接赋值一个size
     vector<int> hash(size);
     int t;
     ; i < n; i++) {
         scanf("%d", &t);
         bool flag = false;
         ; j < size; j++) {
                         int pos = hash[(t + j * j) % size;
             ) {
                 flag = true;
                 hash[(t + j * j) % size] = t;
                 break;
             }
         }
         if (!flag) printf("%d cannot be inserted.\n", t);
     }
     float cmpCnt = 0.0;
     ; i < m; i++) {
         scanf("%d", &t);
         ;
         ; j < size; j++) {
             ) {
                 break;
             }
             cmp++;
         }
         cmpCnt += cmp;
     }
     printf("%.1f", cmpCnt / m);
     ;
 }

[PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)的更多相关文章

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

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

  2. [PAT] 1143 Lowest Common Ancestor(30 分)

    1143 Lowest Common Ancestor(30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  3. PAT 1143 Lowest Common Ancestor[难][BST性质]

    1143 Lowest Common Ancestor(30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  4. PAT Advanced 1143 Lowest Common Ancestor (30) [二叉查找树 LCA]

    题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...

  5. 1143. Lowest Common Ancestor (30)

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  6. PAT 1143 Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  7. PAT Advanced 1145 Hashing – Average Search Time (25) [哈希映射,哈希表,平⽅探测法]

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

  8. 1145. Hashing - Average Search Time (25)

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

  9. PAT 1145 Hashing - Average Search Time [hash][难]

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

随机推荐

  1. lighttpd - 配置文件

    转载其他网站,收藏查看! 配置文件lighttpd.conf参数详细说明的链接和选译 发表于 2010年12月22日 http://redmine.lighttpd.net/projects/ligh ...

  2. jsp电子商务系统之六 订单篇1

    常规一个商品一个订单 多个商品一个订单 订单只有提交才能结算 付款页面 代码实现,主要是Servlet代码和Service业务层的代码,此处业务层,对多个dao的操作更为明显,体现业务二字!!! pa ...

  3. 背景建模技术(七):预处理(PreProcessor)模块

    预处理(PreProcessor)模块是BgsLibrary中一个必选的模块,是真正进入背景建模算法的“预处理”过程,其主要功能包括‘去模糊’.‘获得灰度图’.'应用Canny算子‘等可选模块. 下面 ...

  4. 总结:Bias(偏差),Error(误差),Variance(方差)及CV(交叉验证)

    犀利的开头 在机器学习中,我们用训练数据集去训练(学习)一个model(模型),通常的做法是定义一个Loss function(误差函数),通过将这个Loss(或者叫error)的最小化过程,来提高模 ...

  5. Block中的循环引用警告

  6. ubuntu 服务器搭建汇总

    开启ssh 1.首先:终端安装开启ssh-server服务: sudo apt-get install openssh-server 2.然后确认sshserver是否启动了: ps-e | grep ...

  7. HDU1254 bfs

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  8. 用HTML5 File API 实现截图粘贴上传、拖拽上传

    <!DOCTYPE html><html><head> <title>test chrome paste image</title> < ...

  9. WPF:为什么使用ContentPresenter.ContentSource而不是Content属性?

    因为ContentPresenter.ContentSource比Content属性加一个TemplateBinding看起来更方便?不仅仅是这些,实际上如果用ContentSource的话,Cont ...

  10. 洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2

    https://www.luogu.org/problem/show?pid=2944 题目描述 Wisconsin has had an earthquake that has struck Far ...