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 (≤10​4​​) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then Ndistinct 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 -

题目大意:向一个Hash表中插入不同的数字,并且给出了一个 TSize ,就是哈希的函数,用输入的数字%TSize,如果TSize不是素数,那么就转化为最近的大于它的素数。

按输入顺序输出每一个数的位置,从0开始,如果一个数不能插入(也就是存在冲突问题)那么就输出-。

#include <iostream>
#include <cmath>
#include<vector>
#include <map>
using namespace std; bool isPrime(int m){
int x=sqrt(m)+;
for(int i=;i<x;i++){
if(m%i==)return false;
}
return true;
}
int getPrime(int m){
for(int i=m+;;i++){
if(isPrime(i))return i;
}
}
int main() {
int m,n;
cin>>m>>n;
if(m!=&&!isPrime(m)){
m=getPrime(m);
}
if(m==)m=;
vector<int> vt(n);
for(int i=;i<n;i++){
cin>>vt[i];
}
map<int,int> mp;
for(int i=;i<n;i++){//遍历一个,打印一个就可以解决这个顺序问题。
int x=vt[i]%m;
if(mp[x]==){
cout<<x;
mp[x]=;
}else
cout<<"-";
if(i!=n-)cout<<" ";
} return ;
}

//第一次在牛客网上提交错误,是因为出现了测试:

1 1

1

这是1不是素数,应该把1转化为2才对。在pat上提交,最后一个测试点答案错误。牛客网上的通过率只有10%。。。应该是因为我没有用直接将大数内素数求出来的方法。

看了题解,才知道原来是需要使用二次探查法:

转自:https://www.liuchuo.net/archives/2297

1.如果当前key%value没被占用,那么就放下;

2.如果key%value被占用了,那么就设置一个step从1到value-1,判断(key+step*step)%value是否被占用了,如果未被占用那么就放下;如果都被占用了那么就真的无法放下了。

我的AC:

#include <iostream>
#include <cmath>
#include<vector>
#include <map>
using namespace std; bool isPrime(int m){
int x=sqrt(m)+;
for(int i=;i<x;i++){
if(m%i==)return false;
}
return true;
}
int getPrime(int m){
for(int i=m+;;i++){
if(isPrime(i))return i;
}
}
int main() {
int m,n;
cin>>m>>n;
if(m!=&&!isPrime(m)){
m=getPrime(m);
}
if(m==)m=;
vector<int> vt(n);
for(int i=;i<n;i++){
cin>>vt[i];
}
map<int,int> mp;
for(int i=;i<n;i++){//遍历一个,打印一个就可以解决这个顺序问题。
int x=vt[i]%m;
if(mp[x]==){
cout<<x;
mp[x]=;
}else{
int step=;
bool flag=false;
for(int j=step;j<m;j++){
int y=(vt[i]+j*j)%m;
if(mp[y]==){
cout<<y;
mp[y]=;
flag=true;break;
}
}
if(!flag)cout<<"-";
} if(i!=n-)cout<<" ";
} return ;
}

//学习了二次探查法。

PAT 1078 Hashing[一般][二次探查法]的更多相关文章

  1. PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]

    题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...

  2. PAT 1078. Hashing

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  3. PAT (Advanced Level) 1144~1147:1145Hash二次探查 1146拓扑排序 1147堆

    1144 The Missing Number(20 分) 题意:给定N个数的序列,输出不在序列中的最小的正整数. 分析: 1.给定的N个数可能为正,可能为负,可能重复. 2.由于N≤10​5​​,所 ...

  4. PAT 甲级 1078 Hashing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...

  5. PAT 1145 Hashing - Average Search Time [hash][难]

    1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of d ...

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

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

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

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

  9. PAT 1145 1078| hashing哈希表 平方探测法

    pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...

随机推荐

  1. C++ new delete操作符

    //new delete操作符 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; /* 1.n ...

  2. 监听过多,会抛tooManyListener例外

    在生成一个窗体的时候,点击窗体的右上角关闭按钮激发窗体事件的方法:窗体Frame为事件源,WindowsListener接口调用Windowsclosing(). 为了配合后面的实现,我们必须将Win ...

  3. ArrayList具有数组的查询速度快的优点以及增删速度慢的缺点

    LinkedList接口(在代码的使用过程中和ArrayList没有什么区别) ArrayList底层是object数组,所以ArrayList具有数组的查询速度快的优点以及增删速度慢的缺点. 而在L ...

  4. ytu 1052: 写一函数,将两个字符串连接(水题,指针练习)

    1052: 写一函数,将两个字符串连接 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 343  Solved: 210[Submit][Status][ ...

  5. LoadRunner学习---脚本编写(4)(比较重要)

    今天接着来翻译http://www.wilsonmar.com/中关于LoadRunner脚本编写部分,下面该翻译脚本编写中一些比较重要的部分了. Web用户Action 在VuGen中,脚本产生的默 ...

  6. JQuery------实现鼠标点击和滑动不同效果

    如图: 代码: html <ul class="price-brand-right"> @foreach (Brand item in ViewBag.Brand) { ...

  7. 三、Gradle初级教程——Gradle除了签名打包还能配置jar包

    1.gradle概念 构建工具,Groovy,Java. 2.gradle配置jar包,和libs文件夹导入jar包的区别 到此,还是这种方法导入JAR包比较方便.每次更新JAR包,只需要修改版本号就 ...

  8. 更改hadoop native库文件后datanode故障

    hadoop是用cloudra的官方yum源安装的,服务器是CentOS6.3 64位操作系统,自己写的mapreduce执行的时候hadoop会提示以下错误: WARN util.NativeCod ...

  9. 基于Consul+Upsync+Nginx实现动态负载均衡

    基于Consul+Upsync+Nginx实现动态负载均衡 1.Consul环境搭建 下载consul_0.7.5_linux_amd64.zip到/usr/local/src目录 cd /usr/l ...

  10. ECharts使用(1)(转载)

    转载自http://www.cnblogs.com/Olive116/p/3634480.html 1.  EChart最新的文档目录. 首先创建一个解决方案,目录如下: 之前的一篇文章中讲到如果要使 ...