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

题目大意:就是用二次探查法解决冲突问题。

//这个题给我干懵了。为啥查询15时要多+1次?好奇怪啊。

学习了哈希中解决冲突的几种办法:

1.二次探查法

首先h=hash(x)=x%maxSize;

探查需要:j在[0.maxSize-1]这个区间内,使用公式:

new=(h+j^2)%maxSize;

在查询时:

如果查到一个=-1也就是没有这个数,那么就停止;

如果j已经到了maxSize-1仍旧没有查到,那么就是未出现在哈希表里。

//不过真的不明白为什么这里要多加1次。

并且正常的探查是需要左右同时进行的,形如1*1.-1*1,2*2,-2*2.....以此类推。

代码转自:https://blog.csdn.net/qq_34594236/article/details/79814881

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; bool isPrime(int num) {
if (num < ) return false;
for (int i = ; i <= sqrt(num); i++) {
if (num % i == ) return false;
}
return true;
} int H(int key, int TSize){
return key % TSize;
} 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 = (H(a, msize) + 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++) {
tot++;
int d = j * j;
int tid = (H(a, msize) + d) % msize;
if (table[tid] == a || table[tid] == -) { // 找到或者不存在
founded = true;
break;
}
}
if(founded ==false) {
tot++;
}
} printf("%.1f\n", tot*1.0/m); return ;
}

//真是学习了。

还有一个非常重要的问题,关于段的,就是定义的数组的长度,如果输入是10000,那么将其转换为最近的素数,那只能是10007,所以最好定义数组长度为10010.

PAT 1145 Hashing - Average Search Time [hash][难]的更多相关文章

  1. PAT 1145 Hashing - Average Search Time

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

  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 A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法

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

  5. PAT 甲级 1145 Hashing - Average Search Time

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

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

  7. 1145. Hashing - Average Search Time

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

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

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

  9. PAT_A1145#Hashing - Average Search Time

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

随机推荐

  1. 修改多渠道打包的App名

    archiveNameFormat = '${flavorName}-${projectName}-${versionName}-${versionCode}'

  2. Web项目开发规范文档

    http://www.kancloud.cn/chandler/css-code-guide/51267

  3. ThinkPHP中的验证码不出现的解决办法

    出现这种问题的原因可能是因为代码写的不规范,出现了其他的输出:解决办法: 原代码:     public function captchaAction()    {        $verify = ...

  4. Spring_day01--Spring的bean管理(xml方式)_属性注入介绍

    Spring的bean管理(xml方式) Bean实例化的方式 1 在spring里面通过配置文件 创建对象 2 bean实例化(创建对象)三种方式实现 第一种 使用类的无参数构造创建(重点) Use ...

  5. kafka对比RocketMQ(转)

    淘宝内部的交易系统使用了淘宝自主研发的Notify消息中间件,使用Mysql作为消息存储媒介,可完全水平扩容,为了进一步降低成本,我们认为存储部分可以进一步优化,2011年初,Linkin开源了Kaf ...

  6. ProcessBuilder执行本地命令

    /**关键技术剖析 * 用本命令名和命令的参数选项构造ProcessBuilder对象,它的start方法执行命令,启动一个进程,返回一个Process对象 * ProcessBuilder的envi ...

  7. Python Pypi 修改 国内源(以豆瓣源为例)

    参考:http://pip.readthedocs.io/en/latest/user_guide/#config-file Pypi在国内豆瓣源的地址如下: http://pypi.douban.c ...

  8. 安装PHP扩展-----phpredis

    一.redis介绍 redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了 memcached的不足,它支持存储的value类型相 ...

  9. 常用MySQL函数

    MySQL数据库提供了很多函数包括: 数学函数: 字符串函数: 日期和时间函数: 条件判断函数: 系统信息函数: 加密函数: 格式化函数: 一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ...

  10. mybatis的一对多,多对一,以及多对对的配置和使用

    1.本文章是无意中看见易百教程的Mybatis教程才注意到这个问题,平时都仅仅是在用CRUD,忽略了这方面的问题,真实十分羞愧   2.首先我们开始对mybatis的一对多的探究   根据这个应用场景 ...