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. BZOJ4144 [AMPPZ2014]Petrol 【最短路 + 最小生成树】

    题目链接 BZOJ4144 题解 这题好妙啊,,orz 假设我们在一个非加油站点,那么我们一定是从加油站过来的,我们剩余的油至少要减去这段距离 如果我们在一个非加油站点,如果我们到达不了任意加油站点, ...

  2. BZOJ2791 Rendezvous

    Description给定一个n个顶点的有向图,每个顶点有且仅有一条出边.对于顶点i,记它的出边为(i, a[i]).再给出q组询问,每组询问由两个顶点a.b组成,要求输出满足下面条件的x.y:1. ...

  3. Flex 布局教程:实例篇【转】

    Flex 布局教程:实例篇   作者: 阮一峰 日期: 2015年7月14日 原文:http://www.ruanyifeng.com/blog/2015/07/flex-examples.html ...

  4. acm1217教训

    能用容器去做的用容器做,尽量少用数组,即使自己明确其数量的上届: #include<iostream> #include<cstring> #include<map> ...

  5. Codeforces Round #398 (Div. 2) B,C

    B. The Queue time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  6. C# 生成订单号的几种方式

    public class RandomNumber { public static object _lock = new object(); ; public string GetRandom1() ...

  7. echarts.js中的图表大小自适应

    echarts的图表,如果父级容器的height/width属性设置为百分比的形式,那么echarts就会warning,且不能正常的生成图表.所以div容器的高度宽度必须指定为px,这设计不知道是为 ...

  8. 牛客多校第五场-D-inv

    链接:https://www.nowcoder.com/acm/contest/143/D来源:牛客网 题目描述 Kanade has an even number n and a permutati ...

  9. u3d局域网游戏网络(c# socket select 模型)

    之前写了一篇. 发完之后第二天实际应用到游戏之后还是发现了一些小毛病. 比如网络模块有重复使用(多对象)的情况.所以将静态类该成了普通类. 比如安卓下会有些异常出现导致游戏逻辑不正常.所以网络相关的函 ...

  10. 基数排序——尚未补完的坑QAQ

    基数排序复杂度是(n+b)logn/logb 我们找一个基数 每次处理一部分位 从低位到高位处理 t是出现次数 s是这个桶管辖的起点 然后就可以写了 不过我这里是指针版的 有点难看 #include& ...