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.:平方 ...
随机推荐
- 第二百五十七节,Tornado框架-路由映射,逻辑处理,文件归类配置
Tornado框架-路由映射,逻辑处理,文件归类配置 Tornado框架 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起 ...
- 用ssh和互信链接建立批量执行
main server: 192.168.100.101 sub sever1: 192.168.100.102 sub server2: 192.168.100.103 main server执行脚 ...
- hdu 1233:还是畅通工程(数据结构,图,最小生成树,普里姆(Prim)算法)
还是畅通工程 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- linux下MySQL与jdk安装
允许本地访问端口:iptables -P OUTPUT ACCEPT1.yum install mysql 2.yum install mysql-server 3.删除MySQL数据库中user表中 ...
- Python+selenium之获取文本值和下拉框选择数据
Python+selenium之获取文本值和下拉框选择数据 一.结合实例进行描述 1. 实例如下所示: #新增标签操作 def func_labels(self): self.driver.find_ ...
- 项目期复习总结2:Table, DIV+CSS,标签嵌套规则
文件夹: 1.表格的意义,含义? 2.表格有哪些元素? 3.表格布局,表格布局的优缺点 4.行元素,块元素的差别? 5.标签的合理嵌套及标签的语义性 ① 表格的意义,含义? 表格应该用来展现那些适合表 ...
- 统计nginx进程占用的物理内存
#!/usr/bin/env python #-*- coding:utf-8 -*- ''' 统计nginx进程占用的物理内存 ''' import os import sys import sub ...
- 以打字形式展示placeholder的插件
http://weber.pub/以打字形式展示placeholder的插件/197.html
- python练习题-3
author:headsen chen date: 2018-06-01 15:51:05 习题 31: 作出决定(if + raw_input) [root@localhost py]# cat ...
- c#基础 第五讲
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...