• 1145 Hashing


    The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). The hash function is defined to be ( 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 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 1. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 1.

    Output Specification:

    For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted. where X is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.

    Sample Input:

    4 5 4
    10 6 4 15 11
    11 4 15 2
    
     

    Sample Output:

    15 cannot be inserted.
    2.8

    题意 :

      给出一个散列表的最大长度,(题目要求将长度换算成比最大长度大的最小素数)将N个数字插入到散列表中,如果不能插入则输出“cannot be inserted”,(采用平方探测法)然后再进行M次查询,问查找的平均长度。

    思路:

      首先应该熟悉平方探测法的原理,因为题目中提到(with positive increments only)所以只需要考虑0, 2, 4, 8……这样的整数即可,若散列表中没有所要查询的数字,则需要查找的第一个空的位置或者散列表的最后一个元素后面的那个位置,插入的时候插入到最后一个位置就行了。

    Code:

    #include <iomanip>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    bool isPrime(int num) {
        if (num < 2) return false;
        for (int i = 2; i * i <= num; ++i) {
            if (num % i == 0) return false;
        }
        return true;
    }
    
    int getPos(int key, int d, int MSize) { return (key + d * d) % MSize; }
    
    int main() {
        int MSize, N, M;
        cin >> MSize >> N >> M;
    
        while (!isPrime(MSize)) MSize++;
        vector<int> v(MSize, 0);
    
        int key;
        bool found;
        for (int i = 0; i < N; ++i) {
            cin >> key;
            found = false;
            for (int j = 0; j < MSize; ++j) {
                int pos = getPos(key, j, MSize);
                if (v[pos] == 0) {
                    v[pos] = key;
                    found = true;
                    break;
                }
            }
            if (!found) cout << key << " cannot be inserted." << endl;
        }
    
        int step = 0;
        for (int i = 0; i < M; ++i) {
            int query;
            cin >> query;
            for (int j = 0; j <= MSize; ++j) {
                step++;
                int pos = getPos(query, j, MSize);
                if (v[pos] == query || v[pos] == 0) break;
            }
        }
    
        cout << setiosflags(ios::fixed) << setprecision(1) << step * 1.0 / M
             << endl;
    
        return 0;
    }

    参考:

    https://blog.csdn.net/liuchuo/article/details/79819316

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    GAIN: Gradient Augmented Inpainting Network for Irregular Holes
    Python+Selenium实现对浏览器的自动操作
    python 中的内置类属性__name__和__doc__
    Python 装饰器
    Free-Form Image Inpainting with Gated Convolution
    解决ubuntu安装软件has install-snap change in progress错误
    Image Inpainting for Irregular Holes Using Partial Convolutions
    理解卷积
    Hive中分区表修复问题
    B2B、B2C、C2C、O2O分别是什么意思?
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12694085.html
Copyright © 2020-2023  润新知