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 ...
随机推荐
- JVM体系结构之二:类加载器
一.概述 定义:虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型.类加载和连接的过程都是在运行期间完成的. 二. 类的加载 ...
- JavaScript-Tool:my97datepicker
ylbtech-JavaScript-Tool:my97datepicker 1.返回顶部 1. 2.下载 https://files.cnblogs.com/files/storebook/java ...
- httpd或Nginx负载均衡tomcat
实验环境:CentOS7 #两台tomcat的基本配置如下: [root@webapps localhost]#setenforce 0 [root@webapps localhost]#iptabl ...
- linux命令-tar打包和压缩并用
tar在打包的时候进行压缩 支持 gzip bzip2 xz 格式 -z gzip格式 -j bzip2格式 -J xz格式 压缩打包 [root@wangshaojun ~]# tar -zc ...
- Qt弹出消息对话框
添加头文件 #include <QMessageBox> if(ui->lineEditXStart->text().isEmpty()) //判断lineedit输入内容是否 ...
- Unusual Sequences
题意: 求解合为 y 的总体 gcd 为 x 的正整数非空序列个数. 解法: 特判一下后,原问题等价于合为 s = y/x 的整体gcd为1的正整数序列个数. 1.$ans = \sum_{\sum{ ...
- RStudio 断点调试 进入for循环语句调试
参考: http://www.rstudio.com/ide/docs/debugging/overview 1.进入调试模式 全选代码,点击source即可进入调试模式. 2.进入for 调试 在F ...
- 1、perl学习
1.字符串函数 print chomp chop length uc lc index ord #转符号为ASCII的数字 chr #转数字为ASCII的字母 substr($string,offse ...
- Saving output of a grep into a file with colors
19 down vote favorite 7 I need to save the result of a grep command into a file, but I also want the ...
- Dreamweaver Flash Photoshop网页设计综合应用 (智云科技) [iso] 1.86G
全书共15章,主要包括网页制作基础.Dreamweaver CC网页制作.Photoshop CC网页图像设计.Flash CC网页动画设计以及综合案例实战5个部分.通过本书的学习,不仅能让读者学会三 ...