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

题意:

给定一个序列,用平方探测法解决哈希冲突,然后给出m个数字,如果这个数字不能够被插入就输出”X cannot be inserted.”,然后输出这m个数字的平均查找时间

题解:

找到大于tsize的最小的素数为真正的tsize,然后建立一个tsize长度的数组。首先用平方探测法插入数字a,每次pos = (a + j * j) % tsize,j是从0~tsize-1的数字,如果当前位置可以插入就将a赋值给v[pos],如果一次都没有能够插入成功就输出”X cannot be inserted.”。其次计算平均查找时间,每次计算pos = (a + j * j) % tsize,其中j <= tsize,如果v[pos]处正是a则查找到了,则退出循环,如果v[pos]处不存在数字表示没查找到,那么也要退出循环。每次查找的时候,退出循环之前的j就是这个数字的查找长度。最后ans除以m得到平均查找时间然后输出~

而我看不懂题,也没听说过Quadratic probing平方探测法解决哈希冲突,看来要多扩充知识点了。

  哈希函数构造方法:H(key) = key % TSize (除留余数法)
  处理冲突方法:Hi = (H(key) + di) % TSize (开放地址发——二次方探测再散列)

  其中di为 1*1 , -1*1 , 2*2 , -2*2 , ··· k*k , -k*k (k <= MSize-1)

  题目中提到 with positive increments only 所以我们只需要考虑正增量即可。

看了很多人本题的题解,我发现有些说要前一次 [0,tsize),但后面计数是要  [0,tsize],虽然这样25分也都拿到了,但是,a%t = (a+t*t)%t 不是应该相等的吗?如果相等,又为什么计数时的那个循环里把等号去了就过不了了呢?网上有些的确时两边都统一[0,tsize),但是在计数的时候,没有找到的要多加一次。到底两种思考方式哪个更合理呢?

AC代码:

#include<bits/stdc++.h>
using namespace std;
bool prime(int x){
if(x<=) return false;
for(int j=;j*j<=x;j++){
if(x%j==) return false;
}
return true;
}
int main(){
int t,m,n,x,f;
cin>>t>>m>>n;
while(!prime(t)) t++;
vector<int> a(t);
for(int i=;i<=m;i++){
cin>>x;
f=;
for(int j=;j<t;j++){//j是[0,t)
int y=(x+j*j)%t;
if(a[y]==||a[y]==x){
a[y]=x;
f=;
break;
}
}
if(!f) printf("%d cannot be inserted.\n", x);
}
f=;
for(int i=;i<=n;i++){
int x;
cin>>x;
for(int j=;j<=t;j++){//j是[0,t]
f++;
int y=(x+j*j)%t;
if(a[y]==||a[y]==x){
break;
}
}
}
printf("%.1f",f*1.0/n);
return ;
}

另一种正确代码:

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
bool isPrime(int num) {
if (num < ) return false;
for (int i = ; i *i<=num; i++) {
if (num % i == ) return false;
}
return true;
} int msize, n, m, a, table[];
int main() {
memset(table, -, sizeof(table));
scanf("%d%d%d", &msize, &n, &m); while (isPrime(msize) == false) msize++; for (int i = ; i < n; i++) {
scanf("%d", &a); bool founded = false;
for (int j = ; j < msize; j++) {
int d = j * j;
int tid = (a + d) % msize;
if (table[tid] == -) {
founded = true;
table[tid] = a;
break;
}
}
if (founded == false) {
printf("%d cannot be inserted.\n", a);
}
}
int tot = ; for (int i = ; i < m; i++) {
scanf("%d", &a);
int t = ;
bool founded = false;
for (int j = ; j < msize; j++) {//这边是j从0-msize
tot++;
int d = j * j;
int tid = (a + d) % msize;
if (table[tid] == a || table[tid] == -) { // 找到或者不存在
founded = true;
break;
}
}
if(founded ==false) {//没有找到要多加一次
tot++;
}
} printf("%.1f\n", tot*1.0/m); return ;
}

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

  1. PAT 甲级 1145 Hashing - Average Search Time

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

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

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

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

  4. PAT 甲级 1055 The World's Richest (25 分)(简单题,要用printf和scanf,否则超时,string 的输入输出要注意)

    1055 The World's Richest (25 分)   Forbes magazine publishes every year its list of billionaires base ...

  5. [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 ...

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

  7. PAT甲级:1036 Boys vs Girls (25分)

    PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...

  8. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  9. 1145. Hashing - Average Search Time

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

随机推荐

  1. HDFS日志的查看总结

    HDFS日志查看的两种方式:HDFS安装目录中的logs中和HDFS WEB UI上 HDFS安装目录中的logs中看日志   我们分别在master.slave1以及slave2上安装了HDFS,只 ...

  2. 团队协作editconfig与eslint

    editconfig root = true [*] charset = utf-8 indent_style = space indent_size = 2 end_of_line = lf ins ...

  3. 2019-2020-1 20199312《Linux内核原理与分析》第十二周作业

    实验背景 2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发.在本实验中,学生需要亲手重现攻击来理解该漏洞,并回答一些问题. ...

  4. laravel-nestedset:多级无限分类正确姿势

    laravel-nestedset:多级无限分类正确姿势   laravel-nestedset是一个关系型数据库遍历树的larvel4-5的插件包 目录: Nested Sets Model简介 安 ...

  5. 洛谷 P1004 方格取数 题解

    P1004 方格取数 题目描述 设有 \(N \times N\) 的方格图 \((N \le 9)\),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字\(0\).如下图所示(见样例): ...

  6. 洛谷 P2313 [HNOI2005]汤姆的游戏 题解

    P2313 [HNOI2005]汤姆的游戏 题目描述 汤姆是个好动的孩子,今天他突然对圆规和直尺来了兴趣.于是他开始在一张很大很大的白纸上画很多很多的矩形和圆.画着画着,一不小心将他的爆米花弄撒了,于 ...

  7. P4425 【[HNOI/AHOI2018]转盘】

    颂魔眼中的一眼题我大湖南竟无一人\(AC\) 首先我们考虑一个性质:我们肯定存在一种最优解,满足从某个点出发,一直往前走,不停下来. 证明:我们假设存在一种最优解,是在\(t_i\)的时候到达\(a\ ...

  8. 发现Mathematica中求逆出错

    发现Mathematica中应用Inverse求逆时出错.

  9. 2D到3D视频转换 三维重建

    2D到3D视频转换(也称为2D到立体3D转换和立体转换)是将2D(“平面”)胶片转换为3D形式的过程,几乎在所有情况下都是立体声,因此它是创建图像的过程.每个眼睛来自一个2D图像. 内容 1概述 1. ...

  10. 【转】浅析Linux中的零拷贝技术

    本文探讨Linux中主要的几种零拷贝技术以及零拷贝技术适用的场景.为了迅速建立起零拷贝的概念,我们拿一个常用的场景进行引入: 引文## 在写一个服务端程序时(Web Server或者文件服务器),文件 ...