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)的更多相关文章

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

  3. [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 ...

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

  5. 1145. Hashing - Average Search Time

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

  6. PAT 甲级 1145 Hashing - Average Search Time

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

  7. PAT 1145 Hashing - Average Search Time

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

  8. PAT_A1145#Hashing - Average Search Time

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

  9. PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突

    Hashing - Average Search Time PAT-1145 需要注意本题的table的容量设置 二次探测,只考虑正增量 这里计算平均查找长度的方法和书本中的不同 #include&l ...

随机推荐

  1. Gradle 学习笔记

    配置 Gradle 的环境变量 export GRADLE_HOME=/opt/software/java/gradle-3.1 export PATH=\(PATH:\)GRADLE_HOME/bi ...

  2. release,debug库互调用,32位,64位程序与库互调用

    以下是基于visual studio 2015和cmake的实验 1,debug或release的应用程序都可以调用release的库2,win32和x64的应用和库无法互调用,在VS中链接时会有一堆 ...

  3. 有关Dispose,Finalize,GC.SupressFinalize函数-托管与非托管资源释放的模式

    //这段代码来自官方示例,删除了其中用处不大的细节using System; using System.ComponentModel; /*** * 这个模式搞的这么复杂,目的是:不管使用者有没有手动 ...

  4. Linux内核调试方法总结之调试宏

    本文介绍的内核调试宏属于静态调试方法,通过调试宏主动触发oops从而打印出函数调用栈信息. 1) BUG_ON 查看bug处堆栈内容,主动制造oops Linux中BUG_ON,WARN_ON用于调试 ...

  5. 洛谷P2657 windy数

    传送 裸的数位dp 看这个题面,要求相邻两个数字之差至少为2,所以我们记录当前填的数的最后一位 同时要考虑毒瘤的前导0.如果填的数前面都是0,则这一位填0是合法的. emmm具体的看代码叭 #incl ...

  6. 小程序mpvue中动态切换echarts图表

    如果你用mpvue,而且还想用echarts,那么这个包你可以以来一下 https://github.com/F-loat/mpvue-echarts 考虑到多个页面都休要用,所以抽出来作为一个组件, ...

  7. Eclipse java web项目 ,导入IntelliJ IDEA 完整操作!

    1.如图,这是一个ec项目,是一个ssh框架,搭建一个后台, 我们在idea 新建一个项目:new - project from Existing sources... 要是不放心,你可以做一个文件备 ...

  8. Java 基础-基本数据类型与表达式

    基本数据类型 基本概念 标识符 标识符与内存中的某个位置对应,Java 中标识符的规范如下: 必须由大小写字母.下划线.美元符号.数字组成 首字母只能是大小写字母.下划线.美元符号 变量 变量的值可以 ...

  9. Jmeter的中英文互换

    1.jmeter的中英文互换:为了更深入的了解Jmeter,一般使用英文版的jmeter. 旧版本下载的默认中文较多.新版本5.1的下载后默认英文比较的多. 方法一: 选项->选择一种语言,但是 ...

  10. idea的热部署

    1:先找到你要热部署的tomcat之后 ,在设置tomcat时  先选择 server,里面有On 'Update' action ()  和 On frame deactivation 这两项  都 ...