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

  1. PAT甲级——A1078 Hashing

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

  2. PAT_A1078#Hashing

    Source: PAT A1078 Hashing (25 分) Description: The task of this problem is simple: insert a sequence ...

  3. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  4. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  5. [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)

    局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...

  6. Consistent hashing —— 一致性哈希

    原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...

  7. 一致性 hash 算法( consistent hashing )a

    一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...

  8. PTA Hashing

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

  9. PAT1078 Hashing

    11-散列2 Hashing   (25分) The task of this problem is simple: insert a sequence of distinct positive in ...

随机推荐

  1. jdbc操作根据bean类自动组装sql,天啦,我感觉我实现了hibernate

    场景:需要将从ODPS数仓中计算得到的大额可疑交易信息导入到业务系统的mysql中供业务系统审核.最简单的方式是用阿里云的组件自动进行数据同步了.但是本系统是开放是为了产品化,要保证不同环境的可移植性 ...

  2. Visual Studio2017 数据库架构比较

    一.前言 开发的时候在测试服务器上和线网服务器上面都有我们的数据库,当我们在线网上面修改或者新增一些字段后,线网的数据库也需要更新,这个时候根据表的修改记录,然后在线网上面一个一个增加修改很浪费效率而 ...

  3. Redis常用操作-------Key(键)

    1.DEL key [key ...] 删除给定的一个或多个 key . 不存在的 key 会被忽略. 可用版本: >= 1.0.0 时间复杂度: O(N), N 为被删除的 key 的数量. ...

  4. [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!

    测试mysqld启动mysql server的时候,报如下错误: 2015-12-17 00:46:02 10785 [ERROR] Fatal error: Please read "Se ...

  5. Linux内核分析-系统中断在内核中的实现

    分析system_call中断处理过程 在MenuOS中添加上周所运用到的系统调用 即在Linuxkernel/menu/test.c文件中,添加代码如下: int Mkdir() { const c ...

  6. 转发:Android开发?用C#!!

    转发自 最近偶然在QQ技术群里见到有人提起用C#开发Android,当时我感觉到很诧异:Android不是只能用Java开发吗?何时可以使用C#了?那个群友便告知我:mono. 百度一下吧!搜到了mo ...

  7. mysql外键关联

    主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, 外键可以有重复的, 可以是空值,用来和其他表建立联系用的.所以说,如果谈到了外键,一定是至少涉及到两张表 ...

  8. 软件工程(GZSD2015) 第三次作业提交进度

    第三次作业题目请查看这里:软件工程(GZSD2015)第三次作业 开始进入第三次作业提交进度记录中,童鞋们,虚位以待哈... 2015年4月19号 徐镇.尚清丽,C语言 2015年4月21号 毛涛.徐 ...

  9. 伪静态与重定向--RewriteBase

    RewriteBase用于设置目录级重写的基准URL,即所有的重定向都是基于这个URL.内部重定向可能看不出效果,但是在外部重定向(使用R flag后),如果不手动指定 / 为根目录,那么就会去整个磁 ...

  10. JsTree使用一例

    SearchDesignPatent.treeContainer().jstree({ 'core' : { 'data' : json.data }, }).bind('click.jstree', ...