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. 微软和Google的盈利模式对比分析

    一: 微软和Google是世界上最成功科技巨头之一,但他们之间却有着不同的产品和业务,二者的盈利方式也各有不同,本文将分析和探讨的二者盈利模式的异同. 微软的盈利模式 在1975年由大学肄业的Bill ...

  2. jstack应用-查找CPU飚高的原因

    场景 在系统上线后,经常会遇到运维的同学跑过来说:“这次发版后,cpu线程使用率到一场,到100%了”.这时候不要慌,可以使用堆转储来分析到底是哪个线程引起的. 查找元凶 发现pid=17850的进程 ...

  3. Xcode打包提交至itunes connect后,提交审核成功,随后出现二进制文件无效

    1.问题描述 Xcode打包提交至itunes connect后,提交审核成功,应用处于待审核状态,过了大概半个小时状态更改为二进制文件无效 2.原因分析 2.1 登陆在苹果中预留的邮箱 ---- 邮 ...

  4. 「BZOJ 1924」「SDOI 2010」所驼门王的宝藏「Tarjan」

    题意 一个\(r\times c\)的棋盘,棋盘上有\(n\)个标记点,每个点有三种类型,类型\(1\)可以传送到本行任意标记点,类型\(2\)可以传送到本列任意标记点,类型\(3\)可以传送到周围八 ...

  5. wx-xcx

    一.因测试小程序,搜索了各种测试小程序的注意点,为方便后续查看,暂时整理罗列如下: 1.留意分享功能的权限:如果某页面对游客访问有权限限制,则需要设置取消其分享功能. 2.小程序强制更新功能:新版小程 ...

  6. ExtJS 4.2.1学习笔记(一)——MVC架构与布局

    1       ExtJS入门 1.1     支持所有主流浏览器 调试推荐:chrome.Safari.Firefox 1.2     推荐目录结构 - appname (包含所有程序代码,是根目录 ...

  7. Anya and Cubes 搜索+map映射

    Anya loves to fold and stick. Today she decided to do just that. Anya has n cubes lying in a line an ...

  8. springcloud微服务总结六

    SpringCloud实战7-Config分布式配置管理 分布式环境下的统一配置框架,已经有不少了,比如百度的disconf,阿里的diamand 官方文档对spring Cloud Config的描 ...

  9. Qt 学习之路 2(60):使用 DOM 处理 XML

    Qt 学习之路 2(60):使用 DOM 处理 XML  豆子  2013年8月3日  Qt 学习之路 2  9条评论 DOM 是由 W3C 提出的一种处理 XML 文档的标准接口.Qt 实现了 DO ...

  10. python基础01—基础数据类型

    数据类型 单位换算 最小的单位为bit,表示一个二进制的0或1,一般使用小写的b表示 存储的最小单位为字节(Byte),1B = 8b 1024B = 1KB 1024KB = 1MB 1024MB ...