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 ...
 
随机推荐
- CF889E Mod Mod Mod
			
http://codeforces.com/problemset/problem/889/E 题解 首先我们观察到在每次取模的过程中一定会有一次的结果是\(a_i-1\),因为如果不是,我们可以调整, ...
 - 大数据笔记(十三)——常见的NoSQL数据库之HBase数据库(A)
			
一.HBase的表结构和体系结构 1.HBase的表结构 把所有的数据存到一张表中.通过牺牲表空间,换取良好的性能. HBase的列以列族的形式存在.每一个列族包括若干列 2.HBase的体系结构 主 ...
 - 9 关联管理器(RelatedManager)
			
知识预览: class RelatedManager class RelatedManager "关联管理器"是在一对多或者多对多的关联上下文中使用的管理器.它存在于下面两种情况: ...
 - mvn 与 pom.xml
			
mvn 安装 wget http://mirrors.hust.edu.cn/apache//maven/maven-3/3.0.5/binaries/apache-maven-3.0.5-bin.t ...
 - MVC2: 路由 及 遇到问题记录
			
MVC 路由 重定向 问题记录 1)MVC 路由 入口方法: (Global.asax)Application_Start()--->(App_Start/RouteConfig.cs)Regi ...
 - 控制 input 输入框不能输入中文,即不能在输入框中使用输入法
			
设置输入框的样式,代码如下 <span style="font-size:18px;"><input type = "text" id = & ...
 - 后台管理模板ACE
			
要做系统,界面展示效果较简单,1个开发人员,无美工,无前端! 以前一直用bootstrap来做界面.这次也走的老路!同样适用的bootstrap界面.做完之后,功能上,我还是比较满意的,兼容IE8+, ...
 - 类StringBuffer
			
1字符串声明和创建 public StringBuffer() 无参构造函数 public StringBuffer(int capacity) 指定容量的字符串缓冲区对象 public String ...
 - 关于addEventListener中事件函数的this指向问题
			
看代码: //定义一个可见的盒子用于绑定点击事件 var box = document.getElementById('box'); box.x = 'box' //设置执行函数的对象属性 funct ...
 - 前端 CSS 一些标签默认有padding
			
一个html body标签 默认有 margin外边距属性 比如ul标签,有默认的padding-left值. 那么我们一般在做站的时候,是要清除页面标签中默认的padding和margin.以便于我 ...