pat 1145:

参考链接

Quadratic probing (with positive increments only) is used to solve the collisions.:平方探测法解决冲突

哈希表:H(key)求余数、二次平方探测法解决冲突、求平均查找长度AVL = 所有次数和/n

需要注意点:处理冲突统计查找次数时,如果查找到哈希表最后一个也失败了,那么次数要+1.

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /*
  4. 哈希表:H(key)求余数、二次平方探测法解决冲突、求平均查找长度AVL = All/n
  5. */
  6. const int maxn = 1e5+5;
  7. int Tsize,n,m;
  8. int a[maxn];
  9. int b[maxn];
  10. int hashTable[maxn];
  11. bool isPrime(int x){
  12. if(x < 2) return false;
  13. for(int i=2;i<=sqrt(x);i++){
  14. if(x%i == 0) return false;
  15. }
  16. return true;
  17. }
  18. int HashKey(int key){
  19. return key%Tsize;
  20. }
  21. int main(){
  22. memset(hashTable,-1,sizeof(hashTable));
  23. cin>>Tsize>>n>>m;
  24. while(isPrime(Tsize) == false) Tsize++;
  25. for(int i=0;i<n;i++) cin>>a[i];
  26. for(int i=0;i<m;i++) cin>>b[i];
  27. //插入
  28. for(int i=0;i<n;i++){
  29. bool flag = false;
  30. for(int j=0;j<Tsize;j++){
  31. int idx = (HashKey(a[i]) + j*j) % Tsize;//平方探测法解决冲突
  32. if(hashTable[idx] == -1){
  33. hashTable[idx] = a[i];
  34. flag = true;
  35. break;
  36. }
  37. }
  38. if(flag == false){
  39. printf("%d cannot be inserted.\n",a[i]);
  40. }
  41. }
  42. //查找
  43. int cnt = 0;
  44. for(int i=0;i<m;i++){
  45. bool flag = false;
  46. for(int j=0;j<Tsize;j++){
  47. cnt++;
  48. int idx = (HashKey(b[i]) + j*j) % Tsize;//平方探测法解决冲突 查找下标
  49. if(hashTable[idx] == b[i] || hashTable[idx] == -1 ){
  50. flag = true;
  51. break;
  52. }
  53. }
  54. if(flag == false) cnt++;
  55. }
  56. printf("%.1f",cnt*1.0/m);
  57. return 0;
  58. }

pat 1078 同上

数组存hash表、hash函数求余、平方探测法解决冲突,并且首先哈希表长度为素数。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int Tsize,n;
  4. const int maxn = 1e4+10;
  5. int a[maxn];
  6. int table[maxn];
  7. bool isPrime(int x){
  8. if(x < 2) return false;
  9. for(int i=2;i<=sqrt(x);i++){
  10. if(x%i == 0) return false;
  11. }
  12. return true;
  13. }
  14. int getHash(int key){
  15. return key%Tsize;
  16. }
  17. int main(){
  18. cin>>Tsize>>n;
  19. for(int i=1;i<=n;i++) cin>>a[i];
  20. while(isPrime(Tsize) == false) Tsize++;
  21. bool first = true;
  22. for(int i=1;i<=n;i++){
  23. bool search = false;
  24. for(int j=0;j<Tsize;j++){
  25. int hashIdx = (getHash(a[i]) + j*j)%Tsize;
  26. if(table[hashIdx] == 0){
  27. if(first) {
  28. cout<<hashIdx;
  29. first = false;
  30. }
  31. else cout<<" "<<hashIdx;
  32. search = true;
  33. table[hashIdx] = a[i];
  34. break;
  35. }
  36. }
  37. if(search == false){
  38. if(first){
  39. first = false;
  40. cout<<"-";
  41. }
  42. else{
  43. cout<<" -";
  44. }
  45. }
  46. }
  47. return 0;
  48. }

另补充哈希冲突的处理方法 和 装填因子:



PAT 1145 1078| hashing哈希表 平方探测法的更多相关文章

  1. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  2. PAT甲级1078 Hashing【hash】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 题意: 给定哈希表的大小和n个数,使用 ...

  3. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  4. PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]

    题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...

  5. PAT 甲级 1078 Hashing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...

  6. SDUT 3377 数据结构实验之查找五:平方之哈希表

    数据结构实验之查找五:平方之哈希表 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 给定的一组 ...

  7. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  8. 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)

    题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...

  9. PAT 1078 Hashing[一般][二次探查法]

    1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...

随机推荐

  1. Docker中进入容器命令行及后台运行

    Docker中我们一般会有两种执行命令的方式,一种是直接进入容器的命令行,在终端执行并查看结果,一种是在后台执行,并不会在终端查看结果. 1.进入容器命令行 su root docker run -i ...

  2. Shell—各种括号的用法

    括号的种类 小括号,圆括号 ( ) 中括号,方括号 [ ] 大括号.花括号 { } 一.单小括号 () 1.另开命令组——小括号中的命令将会新开启一个子shell独立顺序运行,所以括号中的变量不能够被 ...

  3. CCSpriteFrameCache的使用

    配置环境:win7+Cocos2d-x.2.0.3+VS2012 CCSpriteFrameCache是帧缓存类. 通过plist文件导入图片 CCSpriteFrameCache::sharedSp ...

  4. 浅谈vue中的计算属性和侦听属性

    计算属性 计算属性用于处理复杂的业务逻辑 计算属性具有依赖性,计算属性依赖 data中的初始值,只有当初始值改变的时候,计算属性才会再次计算 计算属性一般书写为一个函数,返回了一个值,这个值具有依赖性 ...

  5. Linux学习笔记-第19天 结束了。突然感觉配置一个服务好简单的样子

    课程结束了,这本书又过了一遍,感觉学习到了不少的新知识.虽然整个过程老师讲的有点仓促,但回头想想身处于这个知识大爆炸的时代,学习不单要追求知识面宽广,更需要注重学习的效率,某种角度来讲,这也是一种鞭策 ...

  6. SQL查询--索引

    索引概念和作用 索引是建立在表上的可选对象,目的是为了提高查询速度. 如果要在表中查询指定的记录,在没有索引的情况下,必须遍历整个表,而有了索引之后,只需要在索引中找到符合查询条件的索引字段值,就可以 ...

  7. React中添加注释

    React中的注释,其实确切来讲是jsx中的注释: {/*单行注释*/} {/* 多 行 注 释 */}

  8. EEPROM的操作---SPI接口和I2C接口

    参考:http://blog.csdn.net/yuanlulu/article/details/6163106 ROM最初不能编程,出厂什么内容就永远什么内容,不灵活.后来出现了PROM,可以自己写 ...

  9. 划词标注1——使用svg绘制换行文本并自动识别库中字典数据

    业务需求 给出一段文本,自动识别出文本中包含的关键字信息,关键字是库里已知的数据,根据类型的不同显示出不同的颜色 业务分析 1)采用css:文本识别出来后,根据识别出的文本更改对应文本的dom,通过更 ...

  10. struts2文件上传报错

    说明上传的文件为空,检查上传文件名