zoukankan      html  css  js  c++  java
  • PAT1078:Hashing

    1078. Hashing (25)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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 (<=104) 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 -

    思路

    这道题主要考察哈希表的二次探测寻址,公式为 H(key) = (key + step * step) % hashtableSize (1 <= step < hashtableSize)。
    注: 1.1并不是素数。
    2.初始 M = 10000时,调整后的最小素数为10007。所以分配的空间最好大于10007。

    代码
    #include<iostream>
    #include<vector>
    using namespace std;
    
    bool isPrime(const int num)
    {
        if(num == 1)
            return false;
        if(num % 2 == 0 && num != 2)
            return false;
        for(int i = 2; i < num;i++)
        {
            if(num % i == 0)
                return false;
        }
        return true;
    }
    
    int main()
    {
       int M,N;
       while(cin >> M >> N)
       {
           vector<bool> hashtable(10010,false);
           while(!isPrime(M)) M++;
           for(int i = 0; i < N;i++)
           {
               int value;
               cin >> value;
               int pos = value % M;
               if(!hashtable[pos])
               {
                   hashtable[pos] = true;
                   if(i != 0)
                      cout << " ";
                   cout << pos;
               }
               else
               {
                   bool isFound = false;
                   for(int v = 1; v < M;v++)
                   {
                      pos = (value + v * v) % M;
                      if(!hashtable[pos])
                      {
                          isFound = true;
                          hashtable[pos] = true;
                          if(i != 0)
                           cout << " ";
                          cout << pos;
                          break;
                      }
                   }
                   if(!isFound)
                   {
                       if(i != 0)
                        cout << " ";
                       cout << "-";
                   }
               }
           }
           cout << endl;
       }
    }
  • 相关阅读:
    《将博客搬至CSDN》
    Ubuntu 安装 maven
    Ubuntu jdk1.8安装
    spring整合jms
    jms入门
    MySQL 3306端口开启
    黑窗口下mysql导出导入数据库
    PHP 爬虫体验(三)
    解决nvm安装的node使用sudo npm报错的问题
    PHP 爬虫体验(二)
  • 原文地址:https://www.cnblogs.com/0kk470/p/7651473.html
Copyright © 2011-2022 走看看