PAT 1078. Hashing (25)

1078. 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 % 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 -
 1 #include <iostream>
 2 #include <bitset>
 3 #include <cmath>
 4
 5 using namespace std;
 6
 7 int Msize;
 8 bitset<11000> isinsert;
 9
10 bool isPrime(int num)
11 {
12     if (num == 2)
13         return true;
14     else if (num == 0 || num == 1 || num % 2 == 0)
15         return false;
16     int upper = sqrt(num);
17     for (int i = 3; i <= upper; i++)
18         if (num%i == 0)
19             return false;
20     return true;
21 }
22
23 void DecideSize()
24 {
25     while (!isPrime(Msize))
26         Msize++;
27 }
28
29 int HashFunction(int num)
30 {
31     int index = num%Msize;
32     int i = 0;
33     while (isinsert[index])
34     {
35         index += (2 * i + 1);
36         index %= Msize;
37         if (index == num%Msize)
38             return -1;
39         i++;
40     }
41     isinsert.set(index);
42     return index;
43 }
44
45 int main()
46 {
47     int n;
48     cin >> Msize >> n;
49     DecideSize();
50     for (int i = 0; i < n; i++)
51     {
52         int num;
53         cin >> num;
54         int index = HashFunction(num);
55         if (index == -1)
56             cout << "-";
57         else
58             cout << index;
59         if (i < n - 1)
60             cout << " ";
61     }
62 }
时间: 2024-10-12 12:07:41

PAT 1078. Hashing (25)的相关文章

1078. Hashing (25)【Hash + 探测】——PAT (Advanced Level) Practise

题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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(

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

1078. 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 % TSize" where TSize is the

PAT (Advanced Level) 1078. Hashing (25)

二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; const int

PAT Advanced 1078 Hashing (25) [Hash ?次?探查法]

题目 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

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

PAT 1078 Hashing

# include <stdio.h> # include <algorithm> # include <string.h> # include <iostream> # include <math.h> using namespace std; int prime[10100]; int vis[10110]; int cot; void init_prime() { memset(vis,0,sizeof(vis)); cot=0; for(

pat1078. Hashing (25)

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 func

PAT 1070. Mooncake (25)

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival.  Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture.  Now given the inventory amounts and the prices of al

pat09-散列1. Hashing (25)

09-散列1. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 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 fun