09-散列1. Hashing (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
bool h[];
int GetNextPrime(int num){
int i,j;
if(num==){//注意边界
return ;
}
if(num==){//注意边界
return ;
}
for(i=num;;i++){
if(i%==){
continue;
}
for(j=;j*j<=i;j+=){
if(i%j==){
break;
}
}
if(j*j>i){
return i;
}
}
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int msize,n;
scanf("%d %d",&msize,&n);
int i,num,j;
memset(h,false,sizeof(h));
msize=GetNextPrime(msize);
queue<int> q;
for(i=;i<n;i++){
scanf("%d",&num);
num=num%msize;
if(!h[num]){
h[num]=true;
q.push(num);
}
else{
int next;
for(j=;j<=msize/;j++){
next=(num+j*j)%msize;
if(!h[next]){
h[next]=true;
q.push(next);
break;
}
}
if(j>msize/){
q.push(-);
}
}
}
int now;
if(!q.empty()){
now=q.front();
q.pop();
if(now==-){
printf("-");
}
else{
printf("%d",now);
}
}
while(!q.empty()){
now=q.front();
q.pop();
if(now==-){
printf(" -");
}
else{
printf(" %d",now);
}
}
printf("\n");
return ;
}

pat09-散列1. Hashing (25)的更多相关文章

  1. PTA 字符串关键字的散列映射(25 分)

    7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...

  2. PTA 11-散列2 Hashing (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/679 5-17 Hashing   (25分) The task of this pro ...

  3. 11-散列2 Hashing (25 分)

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

  4. 散列(Hash)表入门

    一.概述 以 Key-Value 的形式进行数据存取的映射(map)结构 简单理解:用最基本的向量(数组)作为底层物理存储结构,通过适当的散列函数在词条的关键码与向量单元的秩(下标)之间建立映射关系 ...

  5. PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法

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

  6. PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分)

    PTA数据结构与算法题目集(中文)  7-43字符串关键字的散列映射 (25 分) 7-43 字符串关键字的散列映射 (25 分)   给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义 ...

  7. PTA数据结构与算法题目集(中文) 7-42整型关键字的散列映射 (25 分)

    PTA数据结构与算法题目集(中文)  7-42整型关键字的散列映射 (25 分) 7-42 整型关键字的散列映射 (25 分)   给定一系列整型关键字和素数P,用除留余数法定义的散列函数将关键字映射 ...

  8. Algorithms - Data Structure - Perfect Hashing - 完全散列

    相关概念 散列表 hashtable 是一种实现字典操作的有效数据结构. 在散列表中,不是直接把关键字作为数组的下标,而是根据关键字计算出相应的下标. 散列函数 hashfunction'h' 除法散 ...

  9. Hashing散列注意事项

    Hashing散列注意事项 Numba支持内置功能hash(),只需__hash__()在提供的参数上调用成员函数即可 .这使得添加对新类型的哈希支持变得微不足道,这是因为扩展APIoverload_ ...

随机推荐

  1. GCC 显示所有的warning option

    此事需求的来源是有要求调查,gcc中的那些warning是有问题的,代码必须要修改,那些事需要确认的,就要显示所有的warning选项是什么 查了下,方法如下 显示所有的warning和warning ...

  2. C# 密封(2)

    上一章节说到 sealed  作用于类,那么sealed 作用到方法和成员上面该如何呢. 在C# 中 Sealed作用于方法必须是重写之后的方法.也就是override+sealed.在之后别的类在继 ...

  3. day08.4-samba共享网盘服务

    1. 安装软件:yum   install   samba   -y 2. 新建共享目录物理路径:mkdir   /zizaijiapu 修改配置文件:vim   /etc/samba/smb.con ...

  4. Druid连接池默认配置和坑

    一.公司默认配置 ds_0: !!com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.jdbc.Driver url: ...

  5. spring 学习(二):spring bean 管理--配置文件和注解混合使用

    spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...

  6. 【bzoj3998】弦论 后缀自动机

    Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不同位置的相同子串算作一个. ...

  7. 宽带、ADSL、以太网、PPPoE

    作者:北极链接:https://www.zhihu.com/question/25847423/answer/31563282来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  8. Python之路系列:面向对象初级:静态属性、静态方法、类方法

    一.静态属性 静态属性相当于数据属性. 用@property语法糖装饰器将类的函数属性变成可以不用加括号直接的类似数据属性. 可以封装逻辑,让用户感觉是在调用一个普通的数据属性. 例子: class ...

  9. Sample-Code:Translator

    <h2>My Spanish Translator</h2> <p> Enter your text in English:  </p> <p&g ...

  10. sql随机插入数据--记录

    sql面试题中经常出现一张学生表,表字段有学生ID,学生课程,学生成绩 今天要实测,so,需要有数据,now,随机生成数据,,, create table student ( id ), --编号 c ...