[PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)

1145 Hashing - Average Search Time(25 分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). 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 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 10​4​​. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 10​5 .

Output Specification:
For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted. where X is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.

Sample Input:

4 5 4
10 6 4 15 11
11 4 15 2

Sample Output:

15 cannot be inserted.
2.8

题意:
给定的问题很简单:插入一段不同的正整数序列到hash表中。尝试从表中找到另一组整数序列并输出平均查找时间(找当前键是否在表中的比较次数)。hash函数被定义为H(key)=key%TSize,TSize是hash表的大小。平方探测法(Quadratic probing)被用来解决冲突。
注意表的大小最好是素数(prime)。如果给的size不是素数,你必须重新定义表size为大于使用者给定的size的最小素数。

思路:
1.如果发生冲突则采用平方探测法:
最终位置=(num%size+j*j),j的取值范围是0~size-1
2.如果在j的取值范围内所有的位置都占满了则输出"x cannot be inserted"。
3.考到一个判断素数的方法和一个平方探测方法。

题解:

 1 #include<cstdlib>
 2 #include<cstdio>
 3 #include<vector>
 4 using namespace std;
 5 bool isPrime(int num) {
 6     //给的num可能有<=1的情况
 7     if (num <= 1) return false;
 8     //判断素数
 9     for (int i = 2; i * i <= num; i++) {
10         if (num % i == 0)return false;
11     }
12     return true;
13 }
14 int main() {
15     int size, n, m;
16     scanf("%d %d %d", &size, &n, &m);
17     while (!isPrime(size)) {
18         size++;
19     }
20     //vector可以直接赋值一个size
21     vector<int> hash(size);
22     int t;
23     for (int i = 0; i < n; i++) {
24         scanf("%d", &t);
25         bool flag = false;
26         for (int j = 0; j < size; j++) {
27                         int pos = hash[(t + j * j) % size;
28             if (hash[(t + j * j) % size] == 0) {
29                 flag = true;
30                 hash[(t + j * j) % size] = t;
31                 break;
32             }
33         }
34         if (!flag) printf("%d cannot be inserted.\n", t);
35     }
36     float cmpCnt = 0.0;
37     for (int i = 0; i < m; i++) {
38         scanf("%d", &t);
39         int cmp = 1;
40         for (int j = 0; j < size; j++) {
41             if (hash[(t + j * j) % size] == t || hash[(t + j * j) % size] == 0) {
42                 break;
43             }
44             cmp++;
45         }
46         cmpCnt += cmp;
47     }
48     printf("%.1f", cmpCnt / m);
49     return 0;
50 }

猜你喜欢

转载自www.cnblogs.com/yfzhou/p/9644247.html