PAT甲级1078 Hashing【hash】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592
题意:
给定哈希表的大小和n个数,使用平方探测法解决冲突,为每个数在哈希表中的位置。
如果给定的哈希表的大小不是质数,将其改为最小的比他大的质数。
思路:
比较基础的题目。有两个要注意的点!
1、初始化notprime数组时,需要注意1,也不是质数。特殊处理notprime[1] = true
2、注意考虑prob的边界,应该是哈希表的大小。
因为对于$prob > msize$,设$prob = msize + i$, 则$prob^2 = msize^2 + 2{msize}{i}+i^2$
根据同余$prob^2%msize = i^2%msize$
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int msize, n;
const int maxn = 1e4 + ;
int notprime[maxn * ]; void init()
{
notprime[] = true;
int now = ;
while(now < 1e5){
if(!notprime[now]){
int tmp = now + now;
while(tmp < 1e5){
notprime[tmp] = true;
tmp += now;
}
}
now++;
}
} int h[maxn];
int pos[maxn]; int main()
{
init();
scanf("%d%d", &msize, &n);
while(notprime[msize]){
msize++;
}
for(int i = ; i < n; i++){
int v;
scanf("%d", &v);
int p = v % msize, prob = ;
while(h[(p + prob * prob) % msize] && prob < msize){
prob++;
}
if(h[(p + prob * prob) % msize]){
pos[i] = -;
}
else{
h[(p + prob * prob) % msize] = v;
pos[i] = (p + prob * prob) % msize;
}
} if(pos[] == -){
printf("-");
}
else{
printf("%d", pos[]);
}
for(int i = ; i < n; i++){
if(pos[i] == -){
printf(" -");
}
else{
printf(" %d", pos[i]);
}
}
printf("\n"); return ;
}
PAT甲级1078 Hashing【hash】的更多相关文章
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1078 Hashing
https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...
- 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 1145 1078| hashing哈希表 平方探测法
pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- PAT甲级——A1078 Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
随机推荐
- Java(4)switch选择结构
一.switch结构(开关语句)的语法 switch(表达式 ){--->类型为int.char case 常量1 :--->case 结构可以有多个 //语句块1 break;---& ...
- 2.12 for循环
for循环 像while循环一样,for可以完成循环的功能. 在Python中 for循环可以遍历任何序列的项目,如一个列表或者一个字符串等. for循环的格式 for 临时变量 in 列表或者字符串 ...
- Ubuntu18.04问题记录
1. pip install 包时偶然间碰上了如下问题: Could not install packages due to an EnvironmentError: [Errno 13] 权限不够: ...
- 如何在Window下安装node\npm\cnpm,并安装vue.js,创建项目
1.安装node.js node.js的官方地址为:https://nodejs.org/en/download/. 根据windows版本后,选择要下载的安装包,下载完毕,按照windows一般应用 ...
- Haystack-全文搜索框架
Haystack 1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsear ...
- 绝对定位下margin的作用
以前一直对绝对定位下的margin作用很模糊,今天细看一下 不使用top,left,margin等 <!DOCTYPE html> <html lang="en" ...
- Jmeter_24个常用函数
JMeter提供了很多函数,如果能够熟练使用,可以为脚本带来很多方便. JMeter函数是一种特殊值,可用于除测试计划外的任何组件. 函数调用的格式如下所示:${__functionName(var1 ...
- Docker发布程序那些事
最近使用docker比较频繁,所以也难免碰到一些坑,在这里记录一下,给自己写个笔记 1.docker 1.13.0版本不支持docker stack deploy -c 多个 -c的yml文件合并 如 ...
- 编码 ASCII, GBK, Unicode+utf-8
0. 1.参考 网页编码就是那点事 阮一峰 字符编码笔记:ASCII,Unicode 和 UTF-8 2.总结 美国 ASCII 码 发音: /ˈæski/ :128个字符,只占用了一个字节的后面7位 ...
- C#生成唯一值的方法汇总
生成唯一值的方法很多,下面就不同环境下生成的唯一标识方法一一介绍,作为工作中的一次总结,有兴趣的可以自行测试: https://www.cnblogs.com/xinweichen/p/4287640 ...