PAT甲级真题 1078 Hashing (25分) C++实现(Quadratic probing 平方探测法解决hash冲突)

题目

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 N distinct 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 -

思路

本题关键是理解什么是Quadratic probing?

Quadratic probing(平方探测法)是Hash冲突解决方法,流程如下:

平方探测的整体流程和线性探测基本相似,但是线性探测是没有③:

①根据哈希函数算出Hashval,i 初始化为零,判断HashTable[Hashval]是否被占用,如果没被占用,则Hashval就是根据哈希函数算出的值,跳出平方探测;如果被占用则向右探测(执行②);

②判断HashTable[Hashval + i * i]是否被占用,如果没被占用,则Hashval = Hashval + i * i,跳出平方探测;如果被占用则向左探测(执行③);

③判断HashTable[Hashval - i * i]是否被占用,如果没被占用,则Hashval = Hashval - i * i,跳出平方探测;如果被占用则i++继续向右探测(执行②);

定理:如果使用平方探测,且表的规模是素数,那么当表至少有一半是空的时候,总能插入新的元素。(反过来讲,哪怕表里有一半+1个位置被填上,那么插入都有可能失败)

还有一个重要问题:探测什么时候停止,或者说i的取值范围应该是多少?答案是[0, size-1]。关于为什么,考虑下面两种情况:

第一种,当i取值范围是[0, size-1]时有
newKey = (key + i*i) % size;

第二种,当i取值范围是[size, +∞]时,i可以表示为i = i + k,然后有

newKey = (key + i*i) % size 
       = (key + (size+k)*(size+k)) % size
       = (key + size*size + 2*k*size + k*k) % size
       = (key + k*k) % size

所以i的取值范围为[0,size-1]即可。

参考资料:
哈希查找之平方探测
开放定址法——平方探测(Quadratic Probing)
Limit for quadratic probing a hash table


回到题目,本题说只考虑正向增量,所以若hash[val]冲突则继续考察hash[val+1]、hash[val+4]、hash[val+9]、……、hash[val+(size-1)^2]即可。

利用exist数组表示记录每个位置是否已被占用 。对每个元素,若找到空档则直接输出;否则输出’=’。

注意将表的大小替换为最小质数时,要考虑到1的情况。

代码

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

bool isPrime(int n){
    if (n == 1) {
        return false;
    }
    for (int i=2; i<=sqrt(n); i++){
        if (n % i == 0){
            return false;
        }
    }
    return true;
}
int main(){
    int m, n;
    cin >> m >> n;
    while (!isPrime(m)){
        m++;
    }
    vector<bool> exist(m);
    for (int i=0; i<n; i++){
        int a;
        cin >> a;
        int h = a % m;
        if (i > 0){
            cout << " ";
        }
      //平方探测法避免冲突
        bool flag = false;  //true:找到空位置; false:未找到
        for (int step=0; step<m; step++){
            h = (a + step * step) % m;
            if (!exist[h]){
                flag = true;
                break;
            }
        }
        if (flag){
            exist[h] = true;
            cout << h;
        }
        else{
            cout << "-";
        }
    }
    return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值