2023.12.15

#include 
#include 
#include 

int main() {
    int N, K;
    std::cin >> N >> K;

    std::unordered_map members;
    members.reserve(N); // 预留空间,避免哈希桶的频繁扩容

    for (int i = 0; i < N; i++) {
        std::string id;
        int mileage;
        std::cin >> id >> mileage;
        members[id] = (members[id] < K ? K : members[id]) + mileage;
    }

    int M;
    std::cin >> M;
    for (int i = 0; i < M; i++) {
        std::string query;
        std::cin >> query;
        auto it = members.find(query);
        if (it != members.end()) {
            std::cout << it->second << std::endl;
        } else {
            std::cout << "No Info" << std::endl;
        }
    }

    return 0;
}
#include 
#include 
#include 

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

// 插入节点到二叉搜索树
void insert(TreeNode*& root, int val) {
    if (root == nullptr) {
        root = new TreeNode(val);
        return;
    }
    if (val < root->val) {
        insert(root->left, val);
    } else {
        insert(root->right, val);
    }
}

// 前序遍历
void preorder(TreeNode* root, std::vector& result) {
    if (root == nullptr) {
        return;
    }
    result.push_back(root->val);
    preorder(root->left, result);
    preorder(root->right, result);
}

// 中序遍历
void inorder(TreeNode* root, std::vector& result) {
    if (root == nullptr) {
        return;
    }
    inorder(root->left, result);
    result.push_back(root->val);
    inorder(root->right, result);
}

// 后序遍历
void postorder(TreeNode* root, std::vector& result) {
    if (root == nullptr) {
        return;
    }
    postorder(root->left, result);
    postorder(root->right, result);
    result.push_back(root->val);
}

// 层序遍历
void levelorder(TreeNode* root, std::vector& result) {
    if (root == nullptr) {
        return;
    }
    std::queue q;
    q.push(root);
    while (!q.empty()) {
        TreeNode* node = q.front();
        q.pop();
        result.push_back(node->val);
        if (node->left) {
            q.push(node->left);
        }
        if (node->right) {
            q.push(node->right);
        }
    }
}

int main() {
    int N;
    std::cin >> N;

    TreeNode* root = nullptr;

    for (int i = 0; i < N; i++) {
        int val;
        std::cin >> val;
        insert(root, val);
    }

    std::vector preorderResult, inorderResult, postorderResult, levelorderResult;
    preorder(root
#include 
#include 

int hash_func(int num, int M, int P) {
    return (num % P) % M;
}

std::vector linear_resolving(const std::vector& arr, int M, int P) {
    std::vector hash_table(M, -1); // 初始化散列表
    int total_search_length = 0;
    int total_search_count = 0;
    
    // 插入元素到散列表
    for (int num : arr) {
        int index = hash_func(num, M, P);
        while (hash_table[index] != -1) {
            index = (index + 1) % M; // 线性再散列
        }
        hash_table[index] = num;
    }
    
    // 查找元素
    for (int num : arr) {
        int index = hash_func(num, M, P);
        int search_length = 1;
        while (hash_table[index] != -1 && hash_table[index] != num) {
            index = (index + 1) % M; // 线性再散列
            search_length++;
        }
        total_search_length += search_length;
        total_search_count++;
    }
    
    std::vector avg_search_lengths(2);
    avg_search_lengths[0] = total_search_length / total_search_count; // 查找成功的平均查找长度
    
    int total_search_length_fail = 0;
    int total_search_count_fail = 0;
    for (int i = 0; i < M; i++) {
        if (hash_table[i] == -1) {
            total_search_length_fail += i + 1; // 线性再散列的步数
            total_search_count_fail++;
        }
    }
    avg_search_lengths[1] = total_search_length_fail / total_search_count_fail; // 查找失败的平均查找长度
    
    return avg_search_lengths;
}

int main() {
    int N, M, P;
    std::cin >> N >> M >> P;
    
    std::vector arr(N);
    for (int i = 0; i < N; i++) {
        std::cin >> arr[i];
    }
    
    std::vector avg_search_lengths = linear_resolving(arr, M, P);
    std::cout << avg_search_lengths[0] << "/" << N << std::endl;
    std::cout << avg_search_lengths[1] << "/" << M - N << std::endl;
    
    return 0;
}
#include 
#include 

int hash_func(int num, int M, int P) {
    return (num % P) % M;
}

std::vector linear_resolving(const std::vector& arr, int M, int P) {
    std::vector hash_table(M, -1); // 初始化散列表
    int total_search_length = 0;
    int total_search_count = 0;
    
    // 插入元素到散列表
    for (int num : arr) {
        int index = hash_func(num, M, P);
        while (hash_table[index] != -1) {
            index = (index + 1) % M; // 线性再散列
        }
        hash_table[index] = num;
    }
    
    // 查找元素
    for (int num : arr) {
        int index = hash_func(num, M, P);
        int search_length = 1;
        while (hash_table[index] != -1 && hash_table[index] != num) {
            index = (index + 1) % M; // 线性再散列
            search_length++;
        }
        total_search_length += search_length;
        total_search_count++;
    }
    
    std::vector avg_search_lengths(2);
    avg_search_lengths[0] = total_search_length / total_search_count; // 查找成功的平均查找长度
    
    int total_search_length_fail = 0;
    int total_search_count_fail = 0;
    for (int i = 0; i < M; i++) {
        if (hash_table[i] == -1) {
            total_search_length_fail += i + 1; // 线性再散列的步数
            total_search_count_fail++;
        }
    }
    avg_search_lengths[1] = total_search_length_fail / total_search_count_fail; // 查找失败的平均查找长度
    
    return avg_search_lengths;
}

int main() {
    int N, M, P;
    std::cin >> N >> M >> P;
    
    std::vector arr(N);
    for (int i = 0; i < N; i++) {
        std::cin >> arr[i];
    }
    
    std::vector avg_search_lengths = linear_resolving(arr, M, P);
    std::cout << avg_search_lengths[0] << "/" << N << std::endl;
    std::cout << avg_search_lengths[1] << "/" << M - N << std::endl;
    
    return 0;
}

你可能感兴趣的:(算法,哈希算法)