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. easyui添加删除tooltip

    /** * 扩展两个方法 */$.extend($.fn.datagrid.methods, { /** * 开打提示功能 * @param {} jq * @param {} params 提示消息 ...

  2. Oracle字符串分割Split(超简单一条sql解决)

    ) FROM renyuan where name ='张三' 解决如下问题 我现在有一个字段是存:,,3的,而它对应另一张值集表中.eg; 课程人员表 renyuan id name Course ...

  3. ssh免密码登录的几个注意事项

    1, authorized_keys文件中每个公钥占一行,不能分成多行. 2,文件夹默认权限为600 3,如果遇到奇怪的问题,可以把.ssh/文件全部删掉,重新用ssh-keygen生成.

  4. 【代码备份】pocs.m

    超分辨率算法代码 POCS算法,凸集投影法. pocs.m,没有调用的代码,没看懂..只有这个函数..抱歉. function y = pocs(s,delta_est,factor) % POCS ...

  5. UIViewController三种不同的初始化view的方式

    You can specify the views for a view controller using a Storyboard created in Interface Builder. A s ...

  6. 自学Ajax

    使用Ajax快捷函数 说明 出于简化AJAX开发工作的流程,jQuery提供了若干了快捷函数. 实例 1.显示 test.php 返回值(HTML 或 XML,取决于返回值). $.get(" ...

  7. 判断uiscrollView滑到底部

     本文转载至 http://blog.csdn.net/cerastes/article/details/39612177 -(void)scrollViewDidScroll:(UIScrollVi ...

  8. nmon工具

    下载地址 1.http://nmon.sourceforge.net/pmwiki.php?n=Site.Download tar xvzf  *.tar.gz,需要配置权限 chmod -x,同时配 ...

  9. 160531、SQL优化-索引

    SQL优化有很多方法,今天来说一说数据库索引. 举例说明: 假设有一个图书Book表,里面有字段id,name, isbn等.如果图书数量巨大的话,我们通过isbn查询通常是比较慢的. 添加数据库索引 ...

  10. 如何让ie9.0以下的浏览器支持getElementsByClassName

    如何让ie9.0以下的浏览器支持getElementsByClassName     /** * node是表示dom树的搜索起点,Classname是需要搜索的类名. * 如果传入的节点上已经存在了 ...