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 ( 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 1. 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 1.

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
 
#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
int isPrime(int n){
int sqr = sqrt(1.0*n);
if(n == || n == )
return ;
for(int i = ; i <= sqr; i++){
if(n % i == )
return ;
}
return ;
}
int hashTB[];
int main(){
int Msize, N, M;
scanf("%d%d%d", &Msize, &N, &M);
while(!isPrime(Msize)){
Msize++;
}
fill(hashTB, hashTB + , -);
for(int i = ; i < N; i++){
int loc, key;
scanf("%d", &key);
loc = key % Msize;
if(hashTB[loc] < ){
hashTB[loc] = key;
}else{
int tag = ;
int q = ;
while(q <= Msize && hashTB[(loc + q*q) % Msize] >= ){
q++;
}
if(hashTB[(loc + q*q)%Msize] < ){
hashTB[(loc + q*q)%Msize] = key;
}else{
printf("%d cannot be inserted.\n", key);
}
}
}
int cnt = ;
for(int i = ; i < M; i++){
int key, loc;
scanf("%d", &key);
loc = key % Msize;
int q = ;
cnt++;
if(hashTB[loc] != key && hashTB[loc] >= ){
while(q <= Msize){
if(hashTB[(loc + q*q) % Msize] != key && hashTB[(loc + q*q) % Msize] >= ){
cnt++;
}else{
cnt++;
break;
}
q++;
}
}
}
double ans = 1.0 * cnt / M;
printf("%.1lf", ans);
cin >> N;
}

总结:

1、题意:先将元素用平方探测法插入哈希表,再进行查找元素并计算平均比较次数。

2、平方探测: loc = key mod Tsize, 当有冲突时依次探测 (loc ± di^2) mod Tsize,di从1到Tsize - 1(本题di从1到Tsize,题目要求with positive increments only,则探测时正向探测,每次仅加di^2,不用减)。当依次找遍所有位置仍没有空位时插入失败。

3、查找次数。 1)查找成功的情况:一次就查找成功,或者在平方探测的过程中查找成功,记录比较次数。2)查找失败:在平方探测的过程中找到了一个空位,则说明失败;或者平方探测的di取遍了它的范围,沿途位置都非空但却没有找到key。记录比较次数。

 

1145. Hashing - Average Search Time的更多相关文章

  1. 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 ...

  2. [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)

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

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

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

  4. PAT 甲级 1145 Hashing - Average Search Time

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...

  5. PAT 1145 Hashing - Average Search Time

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

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

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

  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. PAT_A1145#Hashing - Average Search Time

    Source: PAT A1145 Hashing - Average Search Time (25 分) Description: The task of this problem is simp ...

  9. PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突

    Hashing - Average Search Time PAT-1145 需要注意本题的table的容量设置 二次探测,只考虑正增量 这里计算平均查找长度的方法和书本中的不同 #include&l ...

随机推荐

  1. 【git】git add 添加错文件 撤销

     git add 添加 多余文件 这样的错误是由于, 有的时候 可能 git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件 git add 如果添加了错误的文件的话 撤销操 ...

  2. InputFormat的数据划分、Split调度、数据读取

    在执行一个Job的时候,Hadoop会将输入数据划分成N个Split,然后启动相应的N个Map程序来分别处理它们.数据如何划分?Split如何调度(如何决定处理Split的Map程序应该运行在哪台Ta ...

  3. Spark join连接

    内链接

  4. Hbase数据表解析

    demo为表的命名空间,user为表的名字you2个列族,一个为b.一个为o. NAME 为列族名,Replication_SCOPE实现一个远程集群的复制.compression数据压缩的类型 Hb ...

  5. git简介及安装(win10)

    一句话介绍git Git是Linus Torvalds编写,目前是世界上最先进的分布式版本控制系统. git能干什么? 代码备份.还原,版本管理,分支管理,解决冲突,协同开发... 安装git > ...

  6. JQ remove()方法实现似收货地址逐一删除的效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. RestTemplate proxy 设置方式

    RestTemplate restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory() {{ setProxy(new ja ...

  8. python numpy笔记(重要)

    1.np.array 的shape (2,)与(2,1)含义 ndarray.shape:数组的维度.为一个表示数组在每个维度上大小的整数元组.例如二维数组中,表示数组的“行数”和“列数”. ndar ...

  9. 自定义组件Component

    定义compa组件 由4个页面构成 compa.js: compa.json: compa.wxml: compa:wxss: 1.compa.json:在json文件进行自定义组件声明 { &quo ...

  10. Mybatis-java.lang.RuntimeException: org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in sqlmap/User.xml ### Cause: org.apache.ibatis.builder.B

    mappers(映射器) 使用相对于类路径的资源 如:<mapper resource="sqlmap/User.xml" /> 使用完全限定路径 如:<mapp ...