1078 Hashing

Posted ruruozhenhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1078 Hashing相关的知识,希望对你有一定的参考价值。

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 ( 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 (≤) and N (≤) 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 -

 

题意:

  给出一串数字,要求按照二次方探测法,将这串数字插入到一个大小为Msize的容器中去。如果Msize不是素数,则将其换为比Msize大的最小素数。

思路:

  做这道题首先应该清楚二次方探测法的定义:如果occupied[n%Msize]没有被占用,则直接将其插入到这个位置。如果这个位置已经被占用,则index = (n + step * step) % Msize; 直到找到没有被占用的位置。注意index的求法不要写成 index = step * step + n % Msize;

 

Code :

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int isPrime(int x) {
 6     if (x == 0 || x == 1) return false;
 7     for (int i = 2; i * i <= x; ++i) {
 8         if (x % i == 0) return false;
 9     }
10     return true;
11 }
12 
13 int main() {
14     int Msize, n;
15     cin >> Msize >> n;
16     while (!isPrime(Msize)) Msize++;
17 
18     vector<int> occupied(Msize, 0);
19     vector<int> pos(Msize, -1);
20     int temp;
21     for (int i = 0; i < n; ++i) {
22         cin >> temp;
23         for (int j = 0; j < Msize; ++j) {
24             int index = (j * j + temp) % Msize;
25             if (!occupied[index]) {
26                 pos[i] = index;
27                 occupied[index] = 1;
28                 break;
29             }
30         }
31     }
32     for (int i = 0; i < n; ++i) {
33         if (pos[i] != -1) {
34             if (i == 0)
35                 cout << pos[i];
36             else
37                 cout << " " << pos[i];
38         } else {
39             if (i == 0)
40                 cout << "-";
41             else
42                 cout << " "
43                      << "-";
44         }
45     }
46     cout << endl;
47     return 0;
48 }

 

 

 

 

以上是关于1078 Hashing的主要内容,如果未能解决你的问题,请参考以下文章

1078. Hashing (25)-PAT甲级真题

A1078 Hashing (25分)(哈希表平方探测法)

PAT 1078 Hashing[一般][二次探查法]

PAT 1078 Hashing

1078 Hashing

PAT甲级——A1078 Hashing