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 ...
随机推荐
- Timer计时不准确的问题及解决方法
在项目中,需要每隔20ms发送一个RTP数据包.一开始使用的是System.Windows.Forms下的Timer类,但是发现明显延迟了.用StopWatch测了一下,发现它的触发间隔居然不是20m ...
- host大法之GitHub上不去
dns解析慢,github上不去,慢 修改host. windows下路径为:C:\Windows\System32\drivers\etc\hosts Linux下路径:/etc/hosts 加入: ...
- Notepad++列编辑
NotePad++列编辑 工具:Notepad++使用说明:在我们的日常工作中,经常会碰到要修改多行记录,一行行去处理会非常浪费人力,这时候列编辑就是一个很好的解决方法,列编辑在进行数据批量操作时是一 ...
- TomCat 再次发布我的程序
打包成.war的步骤就不说了,之后的配置和上一次的不一样. 在Tomcat的conf下的server.xml文件中,重新配置如下 <Service name="xfwweb" ...
- PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- “Linux内核分析”第五周报告
张文俊+ 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 学习总结 1.给M ...
- LINUX内核分析第八周总结:进程的切换和系统的一般执行过程
一.进程调度与进程切换 1.不同的进程有不同的调度需求 第一种分类: I/O密集型(I/O-bound) 频繁的进行I/O 通常会花费很多时间等待I/O操作的完成 CPU密集型(CPU-bound) ...
- navicat有数据额结构同步
这个功能可能检查两个库的表结构异同,进行表结构构同步,可以生成同步语句. 比如在测试环境表中新增了字段,可以通过这个工具进行表结构同步.
- 面象对象设计原则之三:里氏替换原则(The Liskov Substitution Principle,LSP)
里氏代换原则由2008年图灵奖得主.美国第一位计算机科学女博士Barbara Liskov教授和卡内基·梅隆大学Jeannette Wing教授于1994年提出.其严格表述如下:如果对每一个类型为S的 ...
- ECSHOP后台登陆后一段时间不操作就超时的解决方法
ECSHOP后台登陆后一段时间不操作就超时的解决方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2012-05-27 客户生意比较好,因此比较忙,常常不在电脑前 ...