1078. Hashing (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 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<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)的更多相关文章

  1. PAT1078 Hashing

    11-散列2 Hashing   (25分) The task of this problem is simple: insert a sequence of distinct positive in ...

  2. 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 ...

  3. pat09-散列1. Hashing (25)

    09-散列1. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue The task of ...

  4. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  5. 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 ...

  6. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  7. PAT甲题题解-1078. Hashing (25)-hash散列

    二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...

  8. PAT-1078 Hashing (散列表 二次探测法)

    1078. Hashing The task of this problem is simple: insert a sequence of distinct positive integers in ...

  9. 1078. Hashing (25)

    时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...

随机推荐

  1. @JsonProperty 注解

    是Jackson注解.fastjson有可以用. 作用在字段或方法上,用来对属性的序列化/反序列化,可以用来避免遗漏属性,同时提供对属性名称重命名,比如在很多场景下Java对象的属性是按照规范的驼峰书 ...

  2. ES6学习之函数扩展

    函数默认参数 function test(x = 1, y = 2) { return x + y } test(5, 6) test() 若默认参数在必须参数之前,要想取得默认参数,只有当传入的值为 ...

  3. Python模块-logging模块(一)

    logging模块用来写日志文件 有5个级别,debug(),info(),warning(),error()和critical(),级别最高的为critical() debug()为调试模式,inf ...

  4. qtp ie_hook

    今天要讲的内容是注册异类子控件授予强制HOOK,名字有点抽象,简单的说就是在一个QTP可识别的A类插件窗口对象中存在着B类插件的控件对象, 最常见的例子就是在应用程序中内嵌一个Browser对象子控件 ...

  5. [poj1088]滑雪(二维最长下降子序列)

    解题关键:记忆化搜索 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  6. 高级查询子条件查询filter

    Filter Context 在查询过程中,只判断该文档是否满足条件,只有Yes或者No { "query":{ "bool":{ //布尔关键词 " ...

  7. Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

    DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...

  8. weex前端式写法解决方案---eros

    前言 如果想用前端的方式写一个app怎么办呢? 如果你用的是 React,那么它已经有了一个比较完善的体系跟社区.如果你用的是Vue又不想花费太多时间去重新学习React,那么目前比较靠谱的方案就是w ...

  9. 学习笔记:首次进行JUnit+Ant构建自动的单元测试(一)

    指导博客:https://blog.csdn.net/Cceking/article/details/51692010 基于软件测试的需求,使用JUnit+Ant构建自动的单元测试. IDE:ecli ...

  10. InfoQ —— 腾讯游戏大数据服务场景与应用

    简介 周东祥,本人从2010年毕业进入腾讯互动娱乐部门工作,一直致力在腾讯游戏运营开发工作.先后负责SAP业务受理系统,盗号自助系统,元数据系统以及近2年在腾讯游戏大数据运营开发中积累大量的大数据开发 ...