查一个真的加过的

👁️ 0 人浏览 💬 0 人评论 ❤️ 添加收藏

8 位的布隆过滤器,两个哈希函数:k % 8 和 (k * 3) % 8。已经加进了 17、24、15。

运行下面这段程序:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int h1(int k) { return k % 8; }
int h2(int k) { return (k * 3) % 8; }

void bloom_add(vector<int>& bits, int k) {
    bits[h1(k)] = 1;
    bits[h2(k)] = 1;
}

string bloom_check(const vector<int>& bits, int k) {
    if (bits[h1(k)] == 1 && bits[h2(k)] == 1) return "可能在";
    return "肯定不在";
}

int main() {
    vector<int> bits(8, 0);
    for (int k : {17, 24, 15}) bloom_add(bits, k);
    cout << bloom_check(bits, 15) << endl;
}

(本题用 g++ -std=c++17 -O0 编译。)

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论