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. When should you use a class vs a struct in C++?

    Question: In what scenarios is it better to use a struct vs a class in C++? Answer: The only differe ...

  2. C# 特性学习笔记

    1.自己定义的特性 注意注释!!!!! 2.使用特性 3.特性的用处

  3. Linux常用基本命令:三剑客命令之-awk模式用法(1)

    再次回顾一下,awk基本语法格式: awk [options] 'Pattern {Action}' file1 file2 ··· 之前的文章有讲过两种Pattern(BEGIN, END),本文, ...

  4. 使用 json-server 模拟数据

    1. 先安装 npm install json-server -g 2.查看是否安装成功 json-server -h 3.准备数据,新建一个文件夹 mock,cd mock,在mock下 新建tes ...

  5. Spring表单验证(Spring Validation)

    1.基本介绍 之前在项目中做的后台验证就是Spring Validation,最近闲下来了,就来整理一下. 从Spring3.0开始,Spring MVC中提供了对java校验的API支持.在Spri ...

  6. ComponetOne 2014 v3版本正式发布

    2014年11月18日---ComponentOne Studio Enterprise 2014 v3版全球正式发布.ComponentOne Studio Enterprise是世界知名的Micr ...

  7. Android View 绘制流程

    Android 中 Activity 是作为应用程序的载体存在,代表着一个完整的用户界面,提供了一个窗口来绘制各种视图,当 Activity 启动时,我们会通过 setContentView 方法来设 ...

  8. JavaScript日期排序

    //日期排序 function sortDownDate(a, b) { return Date.parse(a.received) - Date.parse(b.received); } funct ...

  9. 小程序 青少儿书画 利用engineercms作为服务端

    因为很多妈咪们喜欢发布自己宝宝的作品,享受哪些美好时刻,记录亲子创作过程. 为了方便妈咪们展示亲子创作,比如宝宝们画作,涂鸦,书法,作文,其他才艺,特利用engineercms作为服务端,重新设计了一 ...

  10. Django 知识总结(一)

    Django已经学过的知识点: 1. Urls.py 路由系统: 正则 分组匹配 --> 位置参数 分组命名匹配 --> 关键字参数 分级路由 include 给路由起别名 name=&q ...