PAT_A1145#Hashing - Average Search Time

Source:

PAT A1145 Hashing - Average Search Time (25 分)

Description:

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 ( 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 1. 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 1.

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

Keys:

  • 散列(Hash)

Attention:

  • 二次探测法K的范围,0<= k <= Size

Code:

 1 /*
 2 Data: 2019-08-05 20:14:25
 3 Problem: PAT_A1145#Hashing - Average Search Time
 4 AC: 32:04
 5 
 6 题目大意:
 7 哈希表中插入一些列正整数,再查找一系列正整数并计算平均查找时间;
 8 表长为不小于给定表长的最小素数,冲突处理采用二次探测法(只取正K)
 9 
10 输入:
11 第一行给出,表长Size,待插入总数N,待查找总数M,均<=1e4;
12 第二行给出,N个待插入元素<=1e5
13 第三行给出,M个待查找元素<=1e5
14 输出:
15 若N个数中,有无法插入哈希表的,输出之;
16 计算M个数的平均查找时间,保留一位小数;
17 */
18 #include<cstdio>
19 const int M=1e5+10;
20 int ht[M]={0},mp[M]={0};
21 
22 bool IsPrime(int x)
23 {
24     if(x==0 || x==1)
25         return false;
26     for(int i=2; i*i<=x; i++)
27         if(x%i==0)
28             return false;
29     return true;
30 }
31 
32 int main()
33 {
34 #ifdef ONLINE_JUDGE
35 #else
36     freopen("Test.txt", "r", stdin);
37 #endif // ONLINE_JUDGE
38 
39     int T,n,m,x,sum=0;
40     scanf("%d%d%d", &T,&n,&m);
41     while(!IsPrime(T))
42         T++;
43     for(int i=0; i<n; i++)
44     {
45         scanf("%d", &x);
46         for(int j=0; j<=T; j++)
47         {
48             if(ht[(x+j*j)%T]==0)
49             {
50                 ht[(x+j*j)%T]=x;
51                 mp[x]=j+1;
52                 break;
53             }
54         }
55         if(mp[x]==0)
56         {
57             printf("%d cannot be inserted.\n",x);
58             mp[x]=T+1;
59         }
60     }
61     for(int i=0; i<m; i++)
62     {
63         scanf("%d", &x);
64         if(mp[x]==0)
65         {
66             for(int j=0; j<=T; j++){
67                 if(ht[(x+j*j)%T]==0){
68                     sum += (j+1);
69                     break;
70                 }
71             }
72         }
73         else
74             sum += mp[x];
75     }
76     printf("%.1f", 1.0*sum/m);
77 
78     return 0;
79 }

 

posted @ 2019-05-23 19:49  林東雨  阅读(288)  评论(0编辑  收藏  举报