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. 关于JQueryMobile中Slider的一点研究

    滑动条 Slider                 给input的设置一个新的HTML5属性为type="range",可以给页面添加滑动条组件,可以指定它的value值(当前值 ...

  2. 关于Unity的C#基础学习(四)

    一.数组 存放同种类型的一组数据,同类+多个 1.定义 int [] int_set; int_set=new int[10];  //在堆上分配出10个int,int_set是数组的引用变量,指向1 ...

  3. 采用预取(Prefetch)来加速你的网站(转)

    一.DNS预取 如果你像我一样想在网站上有一个Twitter小程序,还有网站分析,再也许一些网页字体,那么你必须要链接到一些其它域名,这意味着你将不得不引发DNS查询.我的建议通常是,不要还没有先适当 ...

  4. 彻底明确Android中AIDL及其使用

    1.为什么要有AIDL? 不管学什么东西,最先得弄明确为什么要有这个东西.不要说存在即是合理.存在肯定合理,可是你还是没有明确. 对于AIDL有一些人的浅显概念就是,AIDL能够跨进程訪问其它应用程序 ...

  5. "reason":"No handler for type [attachment] declared on field [file]" 最完全解决方案

    0.elasticsearch-mapper-attachments 2.3.4安装 mapper-attachments安装方法分两类,在线和离线: 在线安装 bin/elasticsearch-p ...

  6. 在线制作logo

    logoko:http://www.logoko.com.cn/ markmarker:http://emblemmatic.org/markmaker/#/ logomaker:https://lo ...

  7. logback中MDC使用

    今天在项目发现别人写了很多MDC.put("taskid", "testThread/heart/main_heart");或者MDC.put("ta ...

  8. 如何使用phpmyadmin建立外键约束

    之前都是用sql语句进行的主外键的关联,现在用可视化的phpmyadmin感觉方便了很多,但是在做主外键约束的时候却十中找不到操作在哪里.网上搜索的也是千奇百怪五花八门的,都说的很晦涩,很多都说需要使 ...

  9. 【iOS开发】 AudioSession设置, 切换扬声器和听筒详解-保留其他应用音乐(备忘)

    本文转载至 http://blog.sina.com.cn/s/blog_693de6100101f1g8.html (2013-04-10 17:25:24) 转载▼ 标签: audiosessio ...

  10. 《算法竞赛入门经典》学习笔记 2.4.4 C++中的输入输出

    2.4.3 64位整数输入输出long long除了cin,cout,也可以借助于printf和scanf语句,但对应的占位符缺是和平台与编译器相关的:在Linux中,gcc很同意的用%lld:在Wi ...