PAT (Advanced Level) Practice 1145 Hashing - Average Search Time (25分)
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
要做事项如下:
1.用户自定义表不一定是hash表长,需要取最小的大于等于其的素数,可能会超过1e4,为保险起见直接开2e4
2.所谓二次探测是 :
(开放地址发——二次方探测再散列)
其中di = 1,2,4,9,...,(TSize-1)^2
因为题目中的条件限制,所以我们只需要考虑正增量即可。
3.此题其实有误,如果元素不在hash表内查找次数应该是表长,而不是表长+1,为了AC我这里是取表长+1~
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
/* ll sz[200005],n; */
template <typename T> inline void read(T& x)
{
x=0;char ch=getchar();ll f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
/* inline ll query(ll x){ll res=0;while(x){res+=sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x]+=val;x+=lb(x);}}//第x个加上val */
ll Ms,n,m,check,a[20000];
inline bool prime(ll x)
{
if(x==1||x==2)return 1;
for(rg i=2;i*i<=x;i++)
{
if(x%i==0)return 0;
}
return 1;
}
int main()
{
cin>>Ms>>n>>m;
check=Ms;
while(!prime(check))
{
check++;
//cout<<check<<endl;
}
for(rg i=1;i<=n;i++)
{
ll x,flag=0;
cin>>x;
for(rg j=0;j<check;j++)
{
if(!a[(x+j*j)%check])
{
a[(x+j*j)%check]=x,flag=1;
break;
}
}
if(!flag)cout<<x<<" cannot be inserted."<<endl;
}
ll ans=0;
for(rg i=1;i<=m;i++)
{
ll x;
cin>>x;
for(rg j=0;j<=check;j++)
{
ans++;
if(!a[(x+j*j)%check]||a[(x+j*j)%check]==x)break;
}
}
printf("%.1lfn",ans*1.0/m);
while(1)getchar();
return 0;
}
- WCF系列教程之WCF服务宿主与WCF服务部署
- Hello World · GitHub 指南
- React Native的Navigator详解
- WCF系列教程之WCF服务配置工具
- iOS使用自签名证书实现HTTPS请求
- 敲开 GitHub 的大门 · 注册账号
- 机器学习
- GitHub 主页介绍及修改个人信息
- React Native如何消除启动时白屏
- Observer观察者设计模式
- 创建 GitHub 仓库的步骤及方法
- React Native和原生app通信机制详解
- Python高效编程技巧
- 模块和处理程序之通过HttpModule和HttpHandler拦截入站HTTP请求执行指定托管代码模块
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 使用wget递归镜像网站
- 整理Linux中字符串的相关操作技巧
- Ubuntu 16.04下无法安装.deb的解决方法
- 关于linux中系统输入输出的管理详解
- Linux下IP设置脚本的实例及遇到问题解决办法
- Linux与Windows文件互传(VMWare)
- 如何测试Linux下tcp最大连接数限制详解
- Linux中利用Vim对文件进行密码保护的方法详解
- Linux中禁止用户修改/重置密码
- 详解 MAC/Linux Vi配置环境变量及Java环境变量配置
- 扒开DMA映射的内裤
- 如何理解与识别Linux中的文件类型
- ubantu 16.4下Hadoop完全分布式搭建实战教程
- Linux定时删除日志的简单实现方法
- ubuntu如何安装中文输入法