前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >11-散列2 Hashing (25分)

11-散列2 Hashing (25分)

作者头像
AI那点小事
发布2020-04-20 16:16:43
2730
发布2020-04-20 16:16:43
举报
文章被收录于专栏:AI那点小事

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 \% TSizeH(key)=key%TSize where TSizeTSize 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: MSizeMSize (\le 10^4≤10 ​4 ​​ ) and NN (\le MSize≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then NN 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 -


AC代码:

代码语言:javascript
复制
#include <iostream>
#include <cstring>
using namespace std;

class HashTable{
    private:
        int* num;
        int len;
    public:
        HashTable(int len){
            this->len = this->Get_MaxPrime(len);
            num = new int[this->len];
            //cout<<"长度为"<<this->len<<endl; 
            memset(num,0,sizeof(int)*this->len);
        }

        bool IsPrime(int n){
            bool flag = true;
            if ( n <= 1){
                flag = false;
            }else if (n == 2){
                flag = true;
            }else{
                for ( int i = 2 ; i < n ; i++){
                    if ( n % i == 0){
                        flag = false;
                        break;
                    }
                }
            }
            return flag;
        }

        int Get_MaxPrime(int n){
            if ( n == 1 || n == 2){
                return 2;
            }else{
                for ( int i = n ; ; i++){
                    if ( i % 2 == 0){
                        continue;
                    }else{
                        if (this->IsPrime(i)){
                            return i;                   
                        }       
                    }
                }   
            }
        } 

        void Insert(int n){
            int index = n % this->len;
            if (!this->num[index]){
                num[index] = n;
                //cout<<n<<"所在的位置为:"<<index<<endl; 
            }else{
                int next = -1;
                for ( int i = 1 ; i <= this->len/2 ; i++){
                    next = (index + i * i) % this->len;
                    if (!this->num[next]){
                        this->num[next] = n;
                        break;
                    }
                }
                //cout<<n<<"所在的位置为:"<<next<<endl; 
            }
        }

        int Find(int n){
            int result_index = -1;
            for ( int i = 0 ; i < this->len ; i++){
                if (this->num[i] == n){
                    result_index = i;
                    break;
                }
            }
            return result_index;
        }
};

int main()
{
    int Max,N;
    int* buf;
    cin>>Max>>N;
    buf = new int[N];
    HashTable hashtable(Max);
    for ( int i = 0 ; i < N ; i++){
        cin>>buf[i];
        hashtable.Insert(buf[i]);
    }

    for (int i = 0 ; i < N ; i++){
        buf[i] = hashtable.Find(buf[i]);
    }

    if(buf[0] == -1){
        cout<<"-";
    }else{
        cout<<buf[0];
    }
    for(int i = 1 ; i < N ; i++){
        cout<<" ";
        if(buf[i] == -1){
            cout<<"-";
        }else{
            cout<<buf[i];
        }
    }

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档