PAT 1078 Hashing[一般][二次探查法]
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 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[一般][二次探查法]的更多相关文章
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- PAT 1078. Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT (Advanced Level) 1144~1147:1145Hash二次探查 1146拓扑排序 1147堆
1144 The Missing Number(20 分) 题意:给定N个数的序列,输出不在序列中的最小的正整数. 分析: 1.给定的N个数可能为正,可能为负,可能重复. 2.由于N≤105,所 ...
- PAT 甲级 1078 Hashing
https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...
- 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 ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- 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 ...
- 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 ...
- PAT 1145 1078| hashing哈希表 平方探测法
pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...
随机推荐
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- 关于Unity的C#基础学习(四)
一.数组 存放同种类型的一组数据,同类+多个 1.定义 int [] int_set; int_set=new int[10]; //在堆上分配出10个int,int_set是数组的引用变量,指向1 ...
- oracle起定时任务,每隔1秒执行一次
创建一个测试表和一个存储过程: create table a(a date); create or replace procedure test as begin insert into a valu ...
- db2 将逗号分隔数据转换为多值IN列表
将逗号分隔数据转换为多值IN列表 2010-03-15 11:16:59| 分类: 数据库技术|举报|字号 订阅 下载LOFTER我的照片书 | 原文:http://book.cs ...
- 系统管理模块_部门管理_改进_抽取添加与修改JSP页面中的公共代码_在显示层抽取BaseAction_合并Service层与Dao层
系统管理模块_部门管理_改进1:抽取添加与修改JSP页面中的公共代码 commons.jspf <%@ page language="java" import="j ...
- maven项目引入jar包
今天看一下maven项目的创建和具体操作.
- SQL server 数据库升级版本问题解决办法
在升级或安装数据库的时候,会遇到数据库版本不对的问题,无论怎么升级,升级提示成功了,但打开数据库发现还是原来那个版本.甚至出现重装数据库之后,登陆页面已经提示安装的是新版本了,但登陆进去之后,发现数据 ...
- LeetCode 笔记系列 14 N-Queen II [思考的深度问题]
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Servlet3.0文件上传
Servelt3.0文件上传作为一种便捷的文件上传方式很是值得我们去应用的 1.Servlet3.0文件上传使用步骤 浏览器端的要求 表单的提交方法必须是post 必须有一个文件上传组件 <in ...
- 利用CSS3制作淡入淡出动画效果
CSS3新增动画属性“@-webkit-keyframes”,从字面就可以看出其含义——关键帧,这与Flash中的含义一致. 利用CSS3制作动画效果其原理与Flash一样,我们需要定义关键帧处的状态 ...