Hash Function

本文详细介绍了如何使用两种不同的哈希函数进行哈希操作,并通过实例对比了它们在处理冲突时的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

用至少两个不同的Hash函数实现hashing,并比较冲突情况

Input

Each sampled input consists of two lines. In the first line, there are n (<100, 000)denoting the number of elements, and m (<100, 000)denoting the size of the hash table.
In the second line, there are n elements, seperated by spaces.

Output

For each of the input, you should use two kinds of hash function to output the number of collisions. The hash functions are:

1. The division method: h(k) = k mod m

2. The multiplication method: h(k) = floor( m ( kA mod 1)). A is golden ratio, which can be estimated as 0.6180339887

Sample Input

10 3233 65 97 129 161 34 66 98 130 1625 100061 62 63 64 65

Sample Output

8000

HINT

#include <iostream>
#include <cmath>
using namespace std;
#define maxlen 1000100
#define A 0.6180339887
int  a[maxlen];
int b[maxlen];
int main()
{
    int n,m;
    while (cin >> n)
    {
        cin >> m;
        int counta = 0, countb = 0;
        int i;
        for (i = 0; i < maxlen; i++)
        {
            a[i] = -1;
            b[i] = -1;
        }
        int temp;
        for (i = 0; i < n; i++)
        {
            cin >> temp;
            int tempa = temp%m;
            if (a[tempa] == -1)
                a[tempa] = temp;
            else
                counta++;
            int tempb = floor(m*(temp*A -(int)(temp*A)));
            if (b[tempb] == -1)
                b[tempb] = temp;
            else
                countb++;
            }
        cout << counta << endl<< countb << endl;
    }
    return 0;
}
/**************************************************************
    Problem: ****
    User: Avivadepp
    Language: C++
    Result: Accepted
    Time:32 ms
    Memory:9096 kb
****************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值