PAT 1145 Hashing - Average Search Time

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

#include<iostream> //hash表, 平方探测
#include<vector>
using namespace std;
bool isprime(int a){
    if(a<=1) return false;
    for(int i=2; i*i<=a; i++)
        if(a%i==0) return false;
    return true;
}
int main(){
    int n, m, msize;
    cin>>msize>>n>>m;
    while(!isprime(msize)) msize++;
    vector<int> t(msize, 0);
    for(int i=0; i<n; i++){
        int a, flag=0;
        cin>>a;
        for(int j=0; j<msize; j++){
            int pos=(j*j+a)%msize;
            if(t[pos]==0){
                flag=1;
                t[pos]=a;
                break;
            }
        }
        if(flag==0) printf("%d cannot be inserted.\n", a);
    }
    double aver=0.0;
    for(int i=0; i<m; i++){
        int a;
        cin>>a;
        int cnt=1;
        for(int j=0; j<msize; j++){
            int pos=(j*j+a)%msize;
            if(t[pos]==a||t[pos]==0)
                break;
            cnt++;
        }
        aver+=cnt;
    }
    printf("%.1f\n", aver/m);
    return 0;
} 

原文地址:https://www.cnblogs.com/A-Little-Nut/p/9652300.html

时间: 2024-11-05 23:27:22

PAT 1145 Hashing - Average Search Time的相关文章

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

PAT 1108 Finding Average

The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [?1000,1000] and is accurate up to no m

pat 1108 Finding Average(20 分)

1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [?1000,1000]

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(

PAT 1064 Complete Binary Search Tree

1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <vector> 5 #include <algorithm> 6 7 using namespace std; 8 9 class Node { 10 public: 11 int val; 12 Node* left; 13 Node* right; 14 public: 15 Node(): v

PAT Advanced Level 1145

愿明天的题难一点 把我难倒 然后寒假我就可以刷三遍甲级了 这样榜上就会有权顺荣一刷 权顺荣二刷 权顺荣三刷 还有权顺荣 真是棒棒哒!!!深圳地铁prince,你听到了吗???(PS:犯什么呆萌?还不滚去刷题?) 1145 Hashing - Average Search Time(25 分) The task of this problem is simple: insert a sequence of distinct positive integers into a hash table f

1078 Hashing

题意:给出表长和待插入的元素,求每个元素的插入位置,散列函数为H(key)=key%TSize,解决冲突利用平方探测法(只考虑正向偏移). 思路:同1145 Hashing - Average Search Time 代码: #include <cstdio> #include <cmath> #include <unordered_map> using namespace std; bool isPrime(int n) { if(n<=1) return fa

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

[Math] Beating the binary search algorithm – interpolation search, galloping search

From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下,散列整个数据集是不可行的,或者要求既查找位置,又查找数据本身.这个时候,用哈希表就不能实现O(1)的运行时间了.但对有序数组, 采用分治法通常可以实现O(log(n))的最坏运行时间. 在下结论前,有一点值得注意,那就是可以从很多方面“击败”一个算法:所需的空间,所需的运行时间,对底层数据结构的访