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

#include <stdio.h>
#include <algorithm>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=;
int a[maxn];
int msize,n,m,k;
vector<int> v;
bool isprime(int i){
if(i== || i==)return false;
if(i== || i==)return true;
for(int j=;j*j<=i;j++){
if(i%j==)return false;
}
return true;
}
int main(){
fill(a,a+maxn,-);
scanf("%d %d %d",&msize,&n,&m);
while(!isprime(msize)){
msize++;
}
for(int i=;i<n;i++){
int tmp;
scanf("%d",&tmp);
int j;
for(j=;j<msize;j++){
if(a[(tmp+j*j)%msize]==-){
a[(tmp+j*j)%msize]=tmp;
break;
}
}
if(j==msize){
printf("%d cannot be inserted.\n",tmp);
}
}
float cnt=;
for(int i=;i<m;i++){
int tmp;
scanf("%d",&tmp);
int j;
cnt++;
for(j=;j<msize;j++){
if(a[(tmp+j*j)%msize]==tmp || a[(tmp+j*j)%msize]==-){
break;
}
cnt++;
}
}
printf("%.1f",cnt/m);
}

注意点:hash 散列的平方探查法,题目只要求加法。正常是加减都有,规则为(key+k*k)%tsize,当k在0-tsize内都不满足条件时说明该元素无法插入。减法出现小于0时,((key-k*k)%tsize+tsize)%tsize,即不断加tsize的第一个非负数。

第二点,题目的计算平均查询次数,总感觉有问题,当这个数插不进去时要多加一次,不知道为什么。

PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法的更多相关文章

  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 1145 Hashing - Average Search Time [hash][难]

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

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

  4. PAT 1145 Hashing - Average Search Time

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

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

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

  6. PAT Basic 1043 输出PATest (20分)[Hash散列]

    题目 给定⼀个⻓度不超过10000的.仅由英⽂字⺟构成的字符串.请将字符重新调整顺序,按"PATestPATest-."这样的顺序输出,并忽略其它字符.当然,六种字符的个数不⼀定是 ...

  7. PAT_A1145#Hashing - Average Search Time

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

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

  9. PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题

    1043 Is It a Binary Search Tree (25 分)   A Binary Search Tree (BST) is recursively defined as a bina ...

随机推荐

  1. 设计模式-享元模式(FlyWeight)

    一.概念 享元模式是对象的结构模式,它以共享的方式高效的支持大量的细粒度对象,减少对象的数量,并达到节约内存的目的. 享元对象能够做到共享的关键,主要是区分了内部状态和外部状态,内部状态是对象是在建立 ...

  2. Redis的五种数据类型的简单介绍和使用

    1.准备工作: 1.1在Linux下安装Redis  https://www.cnblogs.com/dddyyy/p/9763098.html 1.2启动Redis 先把root/redis的red ...

  3. 10种JavaScript开发者必备的VS Code插件

    摘要: 好的代码插件可以让工作效率翻倍,心情也更加舒畅! 原文:10 Must-have VS Code Extensions for JavaScript Developers 作者:Michael ...

  4. vue项目未加载完成前显示loading...

    1.在Index.html里面加入loading的元素,让loading元素显示,让app元素隐藏 <!DOCTYPE html> <html> <head> &l ...

  5. Schwartz–Zippel lemma

    鬼知道老师从哪儿扒的这东西啊,.... 百度了一下毛都没有啊,维基百科看不懂啊.. 定理 一个$m$元$n$次多项式,在域$F$内随机给每个变量赋值 等于零的概率小于$\dfrac{n}{|F|}$ ...

  6. React中路由的基本使用

    现在我们来搞一搞React中的路由吧,别问我为什么这木喜欢用搞这个字,因为它比较深奥. 注意下面我们使用的是React-Router-DOM React中的路由基本使用还是满简单的,零碎的小东西有点多 ...

  7. 【读书笔记】iOS-PhoneGap

    以前,用PhoneGap平台创建的应用在提交到AppStore中的时候可能会遇到一些问题,不过PhoneGap 0.8.0版本已经很好地解决了这个问题,而且苹果公司也允许将通过PhoneGap构建的应 ...

  8. 【读书笔记】iOS-Interface Builder

    IBOutlet或IBAction符号对编译不产生任何影响,它们只是标记,用于告诉Xcode这些对象可以和UI控件进行关联,以便于在编辑Interface Builder上的UI控件的时候Xcode可 ...

  9. 【读书笔记】iOS-成为一名开发者

    iOS开发者计划是按年付费的,在过期前60天可以开始续费.如果你不续费的话,你将无法发布应用.另外苹果会吊销你的开发者证书和发布证书.最后,苹果将你在iTunes App Store上的所有应用下架. ...

  10. Web API 方法的返回类型、格式器、过滤器

    一.Action方法的返回类型 a) 操作方法的返回类型有四种:void.简单或复杂类型.HttpResponseMessage类型.IHttpActionResult类型. b) 如果返回类型为vo ...