PAT 1145 Hashing - Average Search Time [hash][难]
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 104. 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 105.
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][难]的更多相关文章
- PAT 1145 Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- [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 ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- 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 ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- 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 ...
- 1145. Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...
- 1145. Hashing - Average Search Time (25)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT_A1145#Hashing - Average Search Time
Source: PAT A1145 Hashing - Average Search Time (25 分) Description: The task of this problem is simp ...
随机推荐
- josn 格式 解析
格式为:{"lng":113.534634,"lat":22.274308} 解析步骤如下: <?php $a=json_decode($goods_in ...
- php插入代码数据库
插入代码:<?php $con = mysql_connect("localhost","peter","abc123"); if ( ...
- CSS3 实现厉害的文字和输入框组合效果
最近在忙着弄网站,学到了不少效果,这又是一个厉害的 <html> <head> <meta http-equiv="Content-Type" co ...
- Servlet的线程是不是共享同一个requset对象及servlet多线程
servlet多线程 一,servlet容器如何同时处理多个请求. Servlet采用多线程来处理多个请求同时访问,Servelet容器维护了一个线程池来服务请求.线程池实际上是等待执行代码的一组 ...
- php -- 魔术方法 之 对象输出 : __toString()
对象输出:__toString() 当一个对象被当做字符串进行输出时(echo,print),会调用__toString()方法 <?php //输出对象 class Person{ //属性 ...
- mysql 分页sql
-- pageSize=3 pageIndex total=7 -- 开始行号 startRowNum=(pageIndex-1)*pageSize+1; -- 结束行号 endRowNum=page ...
- 《Linux实验要求》
实验 1:登录和使用基本的 Linux 命令 实验环境: 安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统. 有另外一个无特权用户 student,密码 ...
- mysql更改utf8编码方式
方法1: 一.查看数据库编码格式 1 mysql> show variables like 'character_set_database'; 二.查看数据表的编码格式 1 mysql> ...
- CPictureEx类
CPictueEx类不仅可以显示GIF(包括GIF动画),还可以显示JPEG.BMP.WMF.ICO.CUR等. 参考:https://www.codeproject.com/Articles/142 ...
- servlet和jsp中间的交互
jsp本质上也是一个servlet, 所有的jsp页面最终会编译成一个servlet 1. jsp访问servlet jsp访问servlet比较简单通过get, post的方式直接访问servlet ...