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 ...
随机推荐
- Java常见设计模式之代理模式
指由一个代理主题来操作真实主题,真实主题执行具体的业务操作,而代理主题负责其它相关业务的处理.比如生活中的通过代理访问网络,客户通过网络代理连接网络(具体业务),由代理服务器完成用户权限和访问限制等与 ...
- python超大数计算
In [26]: %time a = 6789**100000CPU times: user 0 ns, sys: 0 ns, total: 0 nsWall time: 6.2 µsIn [27]: ...
- MS-SQL使用xp_cmdshell命令导出数据到excel
exec master..xp_cmdshell 'bcp "select c.Category_Title as 标题,p.Category_Title as 所属分类 from ltbl ...
- [dp]LCS最长公共子序列
https://www.51nod.com/tutorial/course.html#!courseId=4 复杂度:${\rm O}(nm)$ 转移方程: #include<bits/stdc ...
- [HDU1003]最长子序列和
http://acm.hdu.edu.cn/showproblem.php?pid=1003 解题关键:1.最大连续子序列和模板 2.max.end不能使用,在oj中会显示编译错误 3.注意模板中没有 ...
- Servlet编程实例 续4
---------------siwuxie095 JSP+Servlet+JDBC 继续完善登录实例,将校验逻辑改为:从数据库中获取用户信息进行校验 数据库准备 在 Navicat for MySQ ...
- [原创]SQL表值函数:返回自定义时间段的日期数据
跟以往类似,我依旧介绍一个我日常开发遇到的知识点,谨此记录一下,也希望能帮助到一些朋友. 这次我要介绍的是通过SQL函数返回你输入的两个时间点内的日期数据. 效果图如下: 执行函数:SELECT * ...
- Java基础——深入剖析Java中的装箱和拆箱
(转自:http://www.cnblogs.com/dolphin0520/p/3780005.html) 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若 ...
- C#中小数转化为string的问题
在C#中,把小数转化为string, 比如 45.12, 转换为string时,应该就是"45.12" 但是在项目开发中,开发法国的branch时,由于culture使用的是FR- ...
- cf785D(组合数学)
题目链接: http://codeforces.com/problemset/problem/785/D 题意: 左边全为 '(' 右边全为 ')' 且两者数量想等的字符串称为 RSBS. 给出一个由 ...