PAT 甲級 1078 Hashing (25分)

這道題考數據結構這門課的基本知識了

Hash解決衝突的方法: 線性探測法、平方探測法、拉鍊法

線性探查法:
    先對一個素數m取餘結果爲d,得到想要放置的位置,但是這個位置可能已經被放置過了,如果被放置過了,就從d + 1每次增加1的長度,當走到m - 1的地方時再回到0位置,從頭開始再到d - 1,直到找到一個可以放置的位置爲止,如果最終也沒有找到,則hash失敗,可以考慮更大的m

平方探測法:
    先對一個素數m取餘,得到想要放置的位置,但是這個位置可能已經被放置過了,如果被放置過了,就每次增加step^2的長度,step從0取倒m - 1,因爲step=0第一次就是了,因此後面step從1到m-1,直到找到一個可以放置的位置爲止,如果最終也沒有找到,則hash失敗,可以考慮更大的m

注意點:
    題目中給出的m當是1時,試除法判斷它是一個素數,但2纔是最小的素數,因此需要特判一下,否則第二個測試點過不去
#include<iostream>
#include<cmath>
#include<map>
#include<vector>
using namespace std;

map<int,int> hashc;
vector<int> vect;

bool is_prim(int m){
    int sqr = sqrt(m);
    for (int i = 2; i <= sqr; ++i)
        if (m % i == 0)
            return false;
            
    return true;
}

int get_prim(int m){
    
    if (m == 1) return 2;
    if (m == 2 || m == 3) return m;
    while(!is_prim(m)){
        m++;
    }
    return m;
}

int main(){

    int m,n,num;
    cin>>m>>n;
    m = get_prim(m);
    for (int i = 0; i < n; ++i){
        cin >> num;
        if (hashc[num % m] == 0){
            hashc[num % m] ++;
            vect.push_back(num % m);
        }else{
            int flag = 0;
            for (int j = 1; j < m; ++j){
                int index = (num + j * j) % m;
                if (hashc[index] == 0){
                    hashc[index] ++;
                    vect.push_back(index);
                    flag = 1;
                    break;                    
                }
            }
            if (flag == 0)
                vect.push_back(-1);
        }
    }

    for (int i = 0; i < vect.size(); ++i){
        if (i != 0) cout<<" ";  
        if (vect[i] == -1) cout<<'-';
        else cout<<vect[i];
    }

    return 0;
}

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 -

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章