专栏首页AI那点小事11-散列2 Hashing (25分)

11-散列2 Hashing (25分)

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代码:

#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;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 九度OJ——1154Jungle Roads

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of ...

    AI那点小事
  • 06-图2 Saving James Bond - Easy Version (25分)

    This time let us consider the situation in the movie “Live and Let Die” in which...

    AI那点小事
  • 09-排序2 Insert or Merge (25分)

    Insertion sort iterates, consuming one input element each repetition, and growin...

    AI那点小事
  • HDU 4247 Pinball Game 3D(cdq 分治+树状数组+动态规划)

    Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/...

    ShenduCC
  • hdu---(1054)Strategic Game(最小覆盖边)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/3...

    Gxjun
  • 九度OJ——1154Jungle Roads

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of ...

    AI那点小事
  • String Problem(KMP+最小表示法)- HDU 3374

    Give you a string with length N, you can generate N strings by left shifts. For ...

    ACM算法日常
  • HDUOJ-----Computer Transformation

    Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limi...

    Gxjun
  • Leetcode: Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characte...

    卡尔曼和玻尔兹曼谁曼
  • poj-3660-cows contest(不懂待定)

    N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a program...

    瑾诺学长

扫码关注云+社区

领取腾讯云代金券