A1078. Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. 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 (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.
Sample Input:
4 4
10 6 4 15
Sample Output:
0 1 4 -
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int hashTB[] = {,}, loca[];
int isPrime(int n){
int sqr = (int)sqrt(1.0 * n);
if(n == )
return ;
for(int i = ; i <= sqr; i++){
if(n % i == )
return ;
}
return ;
}
int main(){
int N, MSize, temp;
scanf("%d%d", &MSize, &N);
if(isPrime(MSize) == ){
for(int i = MSize + ; ; i++){
if(isPrime(i)){
MSize = i;
break;
}
}
}
for(int i = ; i < N; i++){
int find = ;
scanf("%d", &temp);
if(hashTB[temp % MSize] == ){
hashTB[temp % MSize] = temp;
loca[i] = temp % MSize;
}
else{
for(int j = ; j <= MSize; j++){
if(hashTB[(temp + j * j) % MSize] == ){
hashTB[(temp + j * j) % MSize] = temp;
loca[i] = (temp + j * j) % MSize;
find = ;
break;
}
}
if(find == )
loca[i] = -;
}
}
if(loca[] == -)
printf("-");
else printf("%d", loca[]);
for(int i = ; i < N; i++){
if(loca[i] == -)
printf(" -");
else printf(" %d", loca[i]);
}
cin >> N;
return ;
}
总结:
1、Quadratic probing:平方探测法。 当出现碰撞时,采用 (pos + i^2) mod size的方式探测,pos是已经哈希后的结果,而不是key。i的探测范围从1到Msize。
2、判断素数时,不要忽略1不是素数。
A1078. Hashing的更多相关文章
- PAT甲级——A1078 Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT_A1078#Hashing
Source: PAT A1078 Hashing (25 分) Description: The task of this problem is simple: insert a sequence ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)
局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...
- Consistent hashing —— 一致性哈希
原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...
- 一致性 hash 算法( consistent hashing )a
一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...
- PTA Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT1078 Hashing
11-散列2 Hashing (25分) The task of this problem is simple: insert a sequence of distinct positive in ...
随机推荐
- echarts柱状图标签显示不完全的问题
echarts 柱状图当x轴标签数目超过一定数目时在小尺寸设备上第一个和最后一个标签不显示(不是重叠),axisLabel设置interval:0也不起作用; 解决办法: 这个问题存在于4.0版本以上 ...
- ansible一键部署k8s单机环境
一.虚拟机准备 干净的Centsot7.4.4G内存.2个CPU 最小化安装,最好带虚拟化 二.执行初始化脚本 注意:脚本中配置静态网卡根据实际网卡名称配置,我用的是ens33 可以用 sed -i ...
- webvirtmgr-重命名kvm虚拟机的名称
之前部署了Webvirtmgr平台管理kvm虚拟机,由于虚拟机在创建时名称是顺便起的,后续在虚拟机上部署了部分业务.为了便于管理,最好将虚拟机的名称重置下. 现在说下如何修改kvm中虚拟机的名称: 比 ...
- linux内核分析第八次实验
20135118 罗鹏越 本周学习的是linux内核中的进程调度,之前我们在操作系统中有讲解进程调度的分类,以及一些调度算法.而linux中的进程调度有所不同,首先老师讲解了进程调度和进程调度的时机, ...
- Linux内核分析——字符集总结与分析
一. 设置修改系统.应用默认字符集 1. 查看虚拟机的字符集: 由此可见,该虚拟机的字符集为zh_CN.UTF-8. 2. 查看服务器支持的编码方式 3. 修改字符集类型 上图可见,LANG字符 ...
- AAPT2 error: check logs for details 问题的终究修复
AAPT2 error: check logs for details Process 'command '***\build-tools\27.0.3\aapt.exe'' finished wit ...
- ACL访问控制
/etc/squid/squid.conf 定义语法: acl aclname acltype string acl aclname acltype "file" s ...
- JavaScript 作用域链与闭包
作用域链 在某个作用域访问某个变量或者函数时,会首先在自己的局部环境作用域中搜寻变量或者函数,如果本地局部环境作用域中有该变量或者函数,则就直接使用找到的这个变量值或者函数:如果本地局部环境作用域中没 ...
- RocketMQ事务消息实战
我们以一个订单流转流程来举例,例如订单子系统创建订单,需要将订单数据下发到其他子系统(与第三方系统对接)这个场景,我们通常会将两个系统进行解耦,不直接使用服务调用的方式进行交互.其业务实现步骤通常为: ...
- [CB]2018年中国智能手机市场出货量
Canalys:2018年中国智能手机市场出货量同比跌逾14% https://www.cnbeta.com/articles/tech/813267.htm 市场调研机构Canalys今日公布中国智 ...