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. SQL SERVER2008 R2修改Server Collation--文檔沒細看先存下來.

    HOW TO: RESTORE THE MASTER DATABASE IN SQL SERVER 2012 by Thomas LaRock | Jan 14, 2014 | MSSQL, SQL ...

  2. 如何调用写好的指定模块?——sys.path

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  3. Ubuntu 安装后的配置及美化(二)

    Ubuntu安装后的配置与美化(二) 上篇文章讲了安装ubuntu后的一系列基础的配置,已经可以满足日常的使用了,这篇文章讲一下安装 IDE 及一些其他的配置. 1.安装 SSR 下载 SSR 客户端 ...

  4. 开发效率神器 uTools - 偏前端和 UI

    本文首发于:Bougie's Blog - 效率神器 uTools 前言 今天组内公众号推荐了 Mac 上的效率神器 Alfred. 详情链接:效率神器 Alfred workflow 插件推荐 早上 ...

  5. The server of Apache (三)——网页优化

    在企业中,部署apache后只采用默认的配置参数,会有很多问题,因为那些配置都是针对以前服务器配置的. 一.网页压缩 1.介绍 配置apache的网页压缩功能,是使用Gzip压缩算法来对apache服 ...

  6. linux系统安全及应用——账号安全(用户切换与提权)

    一.su命令切换用户 su uesr 和 su - user 的区别:前者只切换登录人,shell环境还是上一个人的:后者表示注销当前用户,再进入新用户的shell. 查看切换记录:/var/log/ ...

  7. 使用github和hexo搭建静态博客

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 终于写这篇文章了,这是我使用github和hexo搭建博客的一些心得,希望能给大家一点帮助.少走点弯路.刚接触github,只是用来存项目的版本, ...

  8. P2561 [AHOI2002]黑白瓷砖

    $ \color{#0066ff}{ 题目描述 }$ \(\color{#0066ff}{输入格式}\) 文件中以一行的形式存放一个正整数 n , n ≤ 20 . \(\color{#0066ff} ...

  9. Kibana6.x.x源码分析--import相关说明

    /** * 模块的引入规则: * 不带路径的去node_modules中去找: * 带路径的去相应的路径中去找: * 模块可以不加后缀名,node会依次尝试.mjs..js..json..node 这 ...

  10. 事件获取目标 currentTarget target srcElement 三者之间的区别和联系

    currentTarget   指的是触发事件的当前对象,可以是冒泡和捕获的对象,不一定是点击或者鼠标移入等事件的直接触发对象.可以是他的父元素等. target 指的是事件触发的直接对象.IE有兼容 ...