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. hadoop之 hadoop能为企业做什么?

    hadoop是什么? Hadoop是一个开源的框架,可编写和运行分不是应用处理大规模数据,是专为离线和大规模数据分析而设计的,并不适合那种对几个记录随机读写的在线事务处理模式.Hadoop=HDFS( ...

  2. 30个极大提高开发效率的Visual Studio Code插件

    译者按: 看完这篇文章,我打算从 Sublime Text 转到 Visual Studio Code 了! 原文: Immensely upgrade your development enviro ...

  3. Python 基于python编写一些算法程序等

    基于python编写一些算法程序等 by:授客 QQ:1033553122 QQ群:7156436 没特意去研究,只是这对群友在QQ群里(7156436)提出的一些小程序实现.编程题,算法.问题等,本 ...

  4. 《ASP.NET MVC企业实战》(三)MVC开发前奏

    ​ 在上一篇“<ASP.NET MVC企业级实战>(二)MVC开发前奏”中跟随作者大概了解了一些C#3.0和3.5中的新特性.本篇继续以这样的方式来学习C#中的一些特性.   一.C#3. ...

  5. wap2app(三)-- 添加引导页

    1.在client_index.html文件中添加如下代码: <script type="text/javascript"> if(window.plus){ plus ...

  6. mysql的varchar字段可以存储多少个中文字符

    创建数据库,并创建一张表mytb进行测试 ******************************************************************************* ...

  7. React Refs

    React Refs React 支持一种非常特殊的属性 Ref ,你可以用来绑定到 render() 输出的任何组件上. 这个特殊的属性允许你引用 render() 返回的相应的支撑实例( back ...

  8. CSS| 框模型-margin

    CSS margin 属性 设置外边距的最简单的方法就是使用 margin 属性. margin 属性接受任何长度单位,可以是像素.英寸.毫米或 em. margin 可以设置为 auto.更常见的做 ...

  9. 【PAT】B1051 复数乘法(15 分)

    要会使用math函数, 还要注意到用四舍五入的方法判断是否应该输出0.00 #include <math.h> #include<stdio.h> int main() { d ...

  10. C# 动态方法和静态方法的区别

    C# 动态方法和静态方法的区别 (转) 动态方法与静态方法的区别: 1,使用方法上的区别:动态方法,在使用时需要先创建实例,才能调用实例方法,而静态方法则不需要,直接使用即可. 示例代码如下:静态方法 ...