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 two 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. 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
平方探测法先把数据插入,能插入的计算一下找位置花的时间存入p。
计算查找的花费时,如果p不为0,直接加,如果为0,表示表里没有(要么没插入,要么插不进去,没插入的直到找到vis标记为0就返回步数,否则返回msize + 1(循环停止的条件))。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#define Max 100005
using namespace std;
int msize,n,m,d;
double c = ;
int s[],vis[],p[];
bool ispri(int t)
{
if(t <= )return ;
if(t == || t == )return true;
if(t % != && t % != )return false;
for(int i = ;i * i <= t;i += )
{
if(t % i == || t % (i + ) == )return false;
}
return true;
}
int nexpri(int t)
{
while(!ispri(t))t ++;
return t;
}
int insert_(int t)
{
int e = t % msize;
for(int i = ;i < msize;i ++)
{
int ee = (e + i * i) % msize;
if(!vis[ee])
{
s[ee] = t;
vis[ee] = ;
p[t] = i + ;
return ;
}
}
return ;
}
int search_(int t)
{
int e = t % msize;
for(int i = ;i < msize;i ++)
{
int ee = (e + i * i) % msize;
if(!vis[ee] || s[ee] == t)
{
return i + ;
}
}
return msize + ;
}
int main()
{
scanf("%d%d%d",&msize,&n,&m);
msize = nexpri(msize);
for(int i = ;i < n;i ++)
{
scanf("%d",&d);
if(!insert_(d))printf("%d cannot be inserted.\n",d);
}
for(int i = ;i < m;i ++)
{
scanf("%d",&d);
c += p[d] ? p[d] : search_(d);
}
printf("%.1f",c / m);
}
1145. Hashing - Average Search Time (25)的更多相关文章
- 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 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 ...
- [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 [hash][难]
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of d ...
- 1145. Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- 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_A1145#Hashing - Average Search Time
Source: PAT A1145 Hashing - Average Search Time (25 分) Description: The task of this problem is simp ...
- PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突
Hashing - Average Search Time PAT-1145 需要注意本题的table的容量设置 二次探测,只考虑正增量 这里计算平均查找长度的方法和书本中的不同 #include&l ...
随机推荐
- 报错信息:ORA-00979:不是GROUP BY表达式
如图所示 参考:https://blog.csdn.net/linan0930/article/details/16508025 解决方案:即select 列表项中不存在的列可以出现在group by ...
- 大数据笔记(十三)——常见的NoSQL数据库之HBase数据库(A)
一.HBase的表结构和体系结构 1.HBase的表结构 把所有的数据存到一张表中.通过牺牲表空间,换取良好的性能. HBase的列以列族的形式存在.每一个列族包括若干列 2.HBase的体系结构 主 ...
- leetcode-mid-Linked list-2 Add Two Numbers
mycode 87.22% # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x) ...
- vim推荐的光标移动配置文件?
http://roclinux.cn/?p=1466 inoremap jk inoremap ... 参考较好的vim设置文件 : 共享粘贴板: set clipboard+=unnamed 除了映 ...
- 在一个shell中查看管理 任务(前台和后台)/工作jobs 的命令
在一个shell中查看管理 任务(前台和后台)/工作jobs 的命令 jobs是在同一个shell环境而言, 才有意义的. 为什么有jobs这个命令? 是因为, 如果从cmd line运行gui程序时 ...
- redux源码浅入浅出
运用redux有一段时间了,包括redux-thunk和redux-saga处理异步action都有一定的涉及,现在技术栈转向阿里的dva+antd,好用得不要不要的,但是需要知己知彼要对react家 ...
- python 多进程读写文件
import time from multiprocessing import Process, JoinableQueue, cpu_count import csv ####处理一条数据的方法 d ...
- shift()函数
用于对dataframe中的数整体上移或下移, 当为正数时,向下移. 当为负数时,向上移. 缺少的会填充NaN 参考: https://blog.csdn.net/kizgel/article/det ...
- vue中淡入淡出示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 38 是否要使用memory引擎的表
38 是否要使用memory引擎的表 内存表的数据组织结构 create table t1(id int primary key, c int) engine=Memory; create table ...