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. 170907-关于JavaWeb的题

    1. 答案是B.D Servlet 通过调用 init () 方法进行初始化. Servlet 调用 service() 方法来处理客户端的请求. Servlet 通过调用 destroy() 方法终 ...

  2. python分别使用多线程和多进程获取所有股票实时数据

    python分别使用多线程和多进程获取所有股票实时数据   前一天简单介绍了python怎样获取历史数据和实时分笔数据,那么如果要获取所有上市公司的实时分笔数据,应该怎么做呢? 肯定有人想的是,用一个 ...

  3. 嵌入式Linux之gdb配置和使用

    背景: ARM Cortext-A53核+Linux 4.1.12,内核空间64位,用户态32位,gdb版本7.10.1 GDB编译: 1)手动下载gdb-7.10.1.tar.gz源码编译 ./co ...

  4. MVC4:ajax Json 应用

    1)Json基础 2)Json 字符串和Json对象 3)应用例子 4)JsonHelper 1)Json 基础 JSON中对象通过"{}"来标识,一个"{}" ...

  5. 深入理解webpack基本配置(一)

    1. 安装webpack到全局 在学习构建之前,我们来在本地文件新建一个存放项目的文件夹,比如叫demo1这个项目,然后进入demo1该项目的根目录后,执行命令 npm init运行下,一路回车(先简 ...

  6. 5 centos 6.10 三节点安装apache hadoop 2.9.1

    Hadoop 版本: apache hadoop 2.9.1JDK 版本: Oracle JDK1.8集群规划master(1): NN, RM, DN, NM, JHSslave1(2): DN, ...

  7. struts2 基础2 类型转换器

    struts2常用常量的定义与意义 每一次请求都会创建一个新的action,所以struts2的action是线程安全的 拆分struts 为应用指定多个struts配置文件 src 下为各应用配置的 ...

  8. struts2 基础

    框架(frameWork):某一种应用的半成品 struts2: 表现层 处理与页面进行交互的相关功能  hibernate: 持久层 负责业务逻辑数据的持久化  spring: 业务层 负责复杂的业 ...

  9. 自动化测试--利用opencv进行图像识别与定位

    SIFT检测方法 SIFT算法就是把图像的特征检测出来,通过这些特征可以在众多的图片中找到相应的图片 import cv2 #读取图片,以1.png为例 img=cv2.imread('1.png') ...

  10. .aspx和.aspx.cs之间的区别

    在vs里面创建一个web窗体会产生两种文件:后缀是.aspx和.aspx.cs. 简单的来说,.aspx是表现层,可以简单理解为是写html代码的,界面的设计部分:.cs是对应的逻辑代码,再通过特定的 ...