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. 数据库表转换成JavaBean

    本人花了几个小时用C#开发了一款,数据表生成javabean的软件.目前只支持Mysql,内置类型映射器.开源,没有测试. 支持数据库注释,忘了获取表注释,见谅.使用之前配置一下config.xml文 ...

  2. SQL语句也可以重构优化

    真的,不管是程序中的代码可以重构优化,在SQL Server的语句,也是可以的.下面举个例子,在存储过程中,所传入的数据参数不能为空,另外在对数据表进行更新时,所更新的字段如果是空的话,就更新,如果传 ...

  3. 十四、JS同步异步知识点,重点(Node.js-fs模块补充篇)

    (本片文章如果你能耐着性子看我,保证会对同步和异步有一个非常深刻的理解) JavaScript是单线程执行,所谓的单线程呢就是指如果有多个任务就必须去排队,前面任务执行完成后,后面任务再执行.因为Ja ...

  4. 八、Node.js-http模块

    JS代码如下: /* 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi,来处理客户端的请求相应. 不过对 N ...

  5. NSNotification 消息通知的3种方式

    1.Notification Center的概念: 它是一个单例对象,允许当事件发生时通知一些对象,让对象做出相应反应. 它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的. 这 ...

  6. 「BZOJ 2342」「SHOI 2011」双倍回文「Manacher」

    题意 记\(s_R\)为\(s\)翻转后的串,求一个串最长的形如\(ss_Rss_R\)的子串长度 题解 这有一个复杂度明显\(O(n)\)的做法,思路来自网上某篇博客 一个双倍回文串肯定当且仅当本身 ...

  7. 51 nod 1350 斐波那契表示

    每一个正整数都可以表示为若干个斐波那契数的和,一个整数可能存在多种不同的表示方法,例如:14 = 13 + 1 = 8 + 5 + 1,其中13 + 1是最短的表示(只用了2个斐波那契数).定义F(n ...

  8. 如果plsql连接没问题,但程序中报ORA-12504的错误

    说明程序中配置数据库连接的地方没有写tnsnames.ora中的SERVICE_NAME,或者SERVICE_NAME写的有错,检查一下,改正应该就好了

  9. python—Celery异步分布式

    python—Celery异步分布式 Celery  是一个python开发的异步分布式任务调度模块,是一个消息传输的中间件,可以理解为一个邮箱,每当应用程序调用celery的异步任务时,会向brok ...

  10. luoguP4234 最小差值生成树

    https://www.luogu.org/problemnew/show/P4234 按照边的权值从小到大排序,依次加入,并删除能够删除的权值最小的一条边,用 set 维护当前所有边的边权,并查集维 ...