pat1078. Hashing (25)
1078. Hashing (25)
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<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<cmath>
using namespace std;
void getprime(int &msize){
int i=msize,j;
if(msize==||msize==||msize==){
msize=;
return;
}
for(;;i++){
if(i%==){
continue;
}
int maxl=sqrt(msize*1.0);
for(j=;j<=maxl;j+=){
if(i%j==){
break;
}
}
if(j>maxl){
msize=i;
return;
}
}
}
map<int,int> ha;
bool g[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int i,msize,n,num;
scanf("%d %d",&msize,&n);
getprime(msize); //cout<<msize<<endl; for(i=;i<n;i++){
scanf("%d",&num);
ha[i]=num%msize;
}
int half=msize/;
map<int,int>::iterator it=ha.begin();
g[it->second]=true;
printf("%d",it->second);
it++;
for(;it!=ha.end();it++){
if(!g[it->second]){
g[it->second]=true;
printf(" %d",it->second);
}
else{
for(i=;i<=half;i++){
if(!g[(it->second+i*i)%msize]){
g[(it->second+i*i)%msize]=true;
printf(" %d",(it->second+i*i)%msize);
break;
}
}
if(i>half){
printf(" -");
}
}
}
printf("\n");
return ;
}
pat1078. Hashing (25)的更多相关文章
- PAT1078 Hashing
11-散列2 Hashing (25分) The task of this problem is simple: insert a sequence of distinct positive in ...
- 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise
题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...
- pat09-散列1. Hashing (25)
09-散列1. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue The task of ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- 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 ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT甲题题解-1078. Hashing (25)-hash散列
二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...
- PAT-1078 Hashing (散列表 二次探测法)
1078. Hashing The task of this problem is simple: insert a sequence of distinct positive integers in ...
- 1078. Hashing (25)
时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...
随机推荐
- kvm基础 虚拟机内存、CPU调整
转自http://blog.csdn.net/hnhuangyiyang/article/details/50902223 一.调小虚拟机内存 调小虚拟机内存可以动态实现,不用关机1.查看当前内存大小 ...
- C语言学习笔记--enum和sizeof关键字
1.enum关键字 C语言中enum关键字用来定义枚举类型 (1)enum 是 C 语言中的一种自定义类型(2)enum 值是可以根据需要自定义的的整型值(3)第一个定义的 enum 值默认为 0 ( ...
- 3DES加密/解密
/// <summary> /// C#/PHP/JSP 3DES 加密与解密(只支持UTF-8编码) /// </summary> public class Crypto3D ...
- hive sql 查询 Child Error 错误追究
Diagnostic Messages for this Task: java.lang.Throwable: Child Error at org.apache.hadoop.map ...
- Dialog 自定义使用2(进度条)
1: 自定义的Dialog 代码: public class IphoneProgersssDialog extends Dialog { private Context context; priv ...
- Linux Shell 脚本提示:sleep: 无效的时间间隔"1s\r"
问题:编写好的 Shell 脚本在 Linux 执行时提示报错:sleep: 无效的时间间隔"1s\r" : 原因:若再三确认脚本没有写错,可能是原脚本文件在 Win 下创建编写好 ...
- hadoop slf4j-api 1.6.x (or later) is incompatible with this binding
hadoop slf4j-api 1.6.x (or later) is incompatible with this binding 解决方法: 在POM文件最前面加入: <dependenc ...
- IPMITOOL 配置BMC用户设置
IPMITOOL 配置BMC用户设置 本文档共介绍5条ipmi设置user的命令,这些命令需要使用root权限才能使用,其中- H为需要操作的BMC ip,-I lanplus为使用rmcp+协议发送 ...
- 当有“Button1.Attributes.Add("onclick", "return confirm('你确定要保存修改吗?')");”时,验证控件失效的解决方法
同一个页面用Js和服务器验证控件OnClientClick提交问题 实现功能: 点击Button按钮的OnClientClick事件,不会影响服务器验证控件的验证功能 ...
- linux下编译Boost库
下载源码 boost_1_66_0.tar.gz 生成编译工具 # tar axf boost_1_66_0.tar.gz # cd boost_1_66_0 # yum install gcc gc ...