7-3 Unsuccessful Searches (25分)

在这里插入图片描述
The above figure is a question from GRE-CS 2018. It states:

Given an initially empty hash table HT of size 11. The hash function is H(key)=key%7, with linear probing used to resolve the collisions. Now hash the keys 87, 40, 30, 6, 11, 22, 98 and 20 one by one into HT. What is the average search time for unsuccessful searches?

The answer is 6.

Now you are supposed to write a program to solve this kind of problems.

Input Specification:

Each input file contains one test case. For each case, the first line gives 3 positive integers TSize (≤10​3​​, the table size), M (≤TSize, the divisor in the hash function), and N (≤TSize, the number of integers to be inserted). Then N non-negative integers (≤10​4​​) are given in the next line, separated by spaces.

Output Specification:

Print in a line the average search time for unsuccessful searches, after hashing the N integers into the table. The answer must be accurate up to 1 decimal place.

Sample Input 1:

11 7 8
87 40 30 6 11 22 98 20

Sample Output 1:

6.0

Sample Input 2:

3 3 3
81 2 5

Sample Output 2:

4.0

Note: In sample 2, the last test of the original position counts as well.

  • 思路:哈希探测
    TIPS 1:散列函数和冲突处理函数不是一个!!!
    散列函数是第一次用来求pos: x % MSize , 即题中的 H(Key) = key % 7
    而冲突处理函数是针对表长的,可以散列到整张表中,nex = (pos + di) % TSize, 即题中的 nex = (pos + i ) % 11

TIPS 2: 注意审题!!! 题中non-negative!!!不是postive,可能有0插入表,所有初始应将table赋值为-1加以区别

  • T1 code: for()写法
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int table[maxn];

void InsertTable(int x, int MSize, int TSize)
{
    int pos = x % MSize;
    for(int i = 0; i < TSize; ++i)
    {
        int nex = (pos + i) % TSize;
        if(table[nex] == -1)
        {
            table[nex] = x;
            return;
        }
    }
}
int Search(int x, int TSize)
{
    for(int i = 0; i < TSize; ++i)
    {
        int nex = (x + i) % TSize;
        if(table[nex] == -1)
        {
            return i + 1;
        }
    }
    return TSize + 1;
}

int main()
{
    int TSize, MSize, n;
    scanf("%d %d %d", &TSize, &MSize, &n);
    memset(table, -1, sizeof(table));   //考前看一下这个!!审题啊啊啊啊!!!致命!!!
    for(int i = 0; i < n; ++i)
    {
        int tmp;
        scanf("%d", &tmp);
        InsertTable(tmp, MSize, TSize);    //! ! ! 散列函数不是冲突函数:散列函数只用来求最初的pos
    }
    int sumFail = 0;
    // 冲突后可以散列到整张表上
    for(int i = 0; i < MSize; ++i)
    {
        sumFail += Search(i, TSize);
    }
    printf("%.1f", 1.0 * sumFail / MSize);
    return 0;
}

  • T2 code: while() 写法
#include <bits/stdc++.h>
using namespace std;
vector<int> table;

void InsertTable(int x, int MSize, int TSize)
{
    int pos = x % MSize, nex = pos, step = 0;
    while(step < TSize)
    {
        nex = (pos + step) % TSize;
        if(table[nex] == 0)
        {
            table[nex] = x;
            break;
        }
        step++;
    }
}
int Search(int x, int TSize)
{
    int pos = x % TSize, step = 0;
    while(step < TSize)
    {
        int nex = (pos + step) % TSize;
        if(table[nex] == 0)
        {
            break;
        }
        step++;
    }
    return step + 1;
}

int main()
{
    int TSize, MSize, n;
    scanf("%d %d %d", &TSize, &MSize, &n);
    table.resize(TSize);
    for(int i = 0; i < n; ++i)
    {
        int tmp;
        scanf("%d", &tmp);
        InsertTable(tmp, MSize, TSize);
    }
    int sumSearch = 0;
    for(int i = 0; i < MSize; ++i)
    {
        sumSearch += Search(i, TSize);
    }
    printf("%.1f", 1.0 * sumSearch / MSize);
    return 0;
}

发布了316 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42347617/article/details/105251551