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 ( 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 1. 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 1.

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>
#include<cstdio>
#include<math.h>
using namespace std;
int isPrime(int n){
    int sqr = sqrt(1.0*n);
    if(n == 0 || n == 1)
        return 0;
    for(int i = 2; i <= sqr; i++){
        if(n % i == 0)
            return 0;
    }
    return 1;
}
int hashTB[200000];
int main(){
    int Msize, N, M;
    scanf("%d%d%d", &Msize, &N, &M);
    while(!isPrime(Msize)){
        Msize++;
    }
    fill(hashTB, hashTB + 200000, -1);
    for(int i = 0; i < N; i++){
        int loc, key;
        scanf("%d", &key);
        loc = key % Msize;
        if(hashTB[loc] < 0){
            hashTB[loc] = key;
        }else{
            int tag = 0;
            int q = 1;
            while(q <= Msize && hashTB[(loc + q*q) % Msize] >= 0){
                q++;
            }
            if(hashTB[(loc + q*q)%Msize] < 0){
                hashTB[(loc + q*q)%Msize] = key;
            }else{
                printf("%d cannot be inserted.\n", key);
            }
        }
    }
    int cnt = 0;
    for(int i = 0; i < M; i++){
        int key, loc;
        scanf("%d", &key);
        loc = key % Msize;
        int q = 1;
        cnt++;
        if(hashTB[loc] != key && hashTB[loc] >= 0){
            while(q <= Msize){
                if(hashTB[(loc + q*q) % Msize] != key && hashTB[(loc + q*q) % Msize] >= 0){
                    cnt++;
                }else{
                    cnt++;
                    break;
                }
                q++;
            }
        }
    }
    double ans = 1.0 * cnt / M;
    printf("%.1lf", ans);
    cin >> N;
}
View Code

总结:

1、题意:先将元素用平方探测法插入哈希表,再进行查找元素并计算平均比较次数。

2、平方探测: loc = key mod Tsize, 当有冲突时依次探测 (loc ± di^2) mod Tsize,di从1到Tsize - 1(本题di从1到Tsize,题目要求with positive increments only,则探测时正向探测,每次仅加di^2,不用减)。当依次找遍所有位置仍没有空位时插入失败。

3、查找次数。 1)查找成功的情况:一次就查找成功,或者在平方探测的过程中查找成功,记录比较次数。2)查找失败:在平方探测的过程中找到了一个空位,则说明失败;或者平方探测的di取遍了它的范围,沿途位置都非空但却没有找到key。记录比较次数。

 

转载于:https://www.cnblogs.com/zhuqiwei-blog/p/9569736.html

一、基础信息 数据集名称:Bottle Fin实例分割数据集 图片数量: 训练集:4418张图片 验证集:1104张图片 总计:5522张图片 分类类别: - 类别0: 数字0 - 类别1: 数字1 - 类别2: 数字2 - 类别3: 数字3 - 类别4: 数字4 - 类别5: 数字5 - 类别6: Bottle Fin 标注格式:YOLO格式,包含多边形坐标,适用于实例分割任务。 数据格式:图片格式常见如JPEG或PNG,具体未指定。 二、适用场景 实例分割AI模型开发:数据集支持实例分割任务,帮助构建能够精确识别和分割图像中多个对象的AI模型,适用于对象检测和分割应用。 工业自动化与质量控制:可能应用于制造、物流或零售领域,用于自动化检测和分类物体,提升生产效率。 计算机视觉研究:支持实例分割算法的学术研究,促进目标检测和分割技术的创新。 教育与实践培训:可用于高校或培训机构的计算机视觉课程,作为实例分割任务的实践资源,帮助学生理解多类别分割。 三、数据集优势 多类别设计:包含7个不同类别,涵盖数字和Bottle Fin对象,增强模型对多样对象的识别和分割能力。 高质量标注:标注采用YOLO格式的多边形坐标,确保分割边界的精确性,提升模型训练效果。 数据规模适中:拥有超过5500张图片,提供充足的样本用于模型训练和验证,支持稳健的AI开发。 即插即用兼容性:标注格式直接兼容主流深度学习框架(如YOLO),便于快速集成到各种实例分割项目中。
C:\Users\qqian\AppData\Local\Programs\Python\Python314\Lib\site-packages\setuptools\_distutils\cmd.py:90: SetuptoolsDeprecationWarning: setup.py install is deprecated. !! ******************************************************************************** Please avoid running ``setup.py`` directly. Instead, use pypa/build, pypa/installer or other standards-based tools. This deprecation is overdue, please update your project and remove deprecated calls to avoid build errors in the future. See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details. ******************************************************************************** !! self.initialize_options() running build running build_py copying src\binwalk\__init__.py -> build\lib\binwalk copying src\binwalk\__main__.py -> build\lib\binwalk copying src\binwalk\magic\animation -> build\lib\binwalk\magic copying src\binwalk\magic\archives -> build\lib\binwalk\magic copying src\binwalk\magic\binarch -> build\lib\binwalk\magic copying src\binwalk\magic\bincast -> build\lib\binwalk\magic copying src\binwalk\magic\binwalk -> build\lib\binwalk\magic copying src\binwalk\magic\bootloaders -> build\lib\binwalk\magic copying src\binwalk\magic\code -> build\lib\binwalk\magic copying src\binwalk\magic\compressed -> build\lib\binwalk\magic copying src\binwalk\magic\console -> build\lib\binwalk\magic copying src\binwalk\magic\crypto -> build\lib\binwalk\magic copying src\binwalk\magic\ebml -> build\lib\binwalk\magic copying src\binwalk\magic\ecos -> build\lib\binwalk\magic copying src\binwalk\magic\efi -> build\lib\binwalk\magic copying src\binwalk\magic\encoding -> build\lib\binwalk\magic copying src\binwalk\magic\executables -> build\lib\binwalk\magic copying src\binwalk\magic\filesystems -> build\lib\binwalk\magic copying src\binwalk\magic\firmware -> build\lib\binwalk\magic copying src\binwalk\magic\hashing -> build\lib\binwalk\magic copying src\binwalk\magic\images -> build\lib\binwalk\magic copying src\binwalk\magic\linux -> build\lib\binwalk\magic copying src\binwalk\magic\lzma -> build\lib\binwalk\magic copying src\binwalk\magic\misc -> build\lib\binwalk\magic copying src\binwalk\magic\network -> build\lib\binwalk\magic copying src\binwalk\magic\phones -> build\lib\binwalk\magic copying src\binwalk\magic\sql -> build\lib\binwalk\magic copying src\binwalk\magic\vxworks -> build\lib\binwalk\magic copying src\binwalk\config\extract.conf -> build\lib\binwalk\config copying src\binwalk\plugins\arcadyan.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\cpio.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\dlromfsextract.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\gzipextract.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\gzipvalid.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\jffs2valid.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\lzmaextract.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\lzmavalid.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\tar.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\ubivalid.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\unjffs2.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\unpfs.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\ziphelper.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\zlibextract.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\zlibvalid.py -> build\lib\binwalk\plugins copying src\binwalk\modules\compression.py -> build\lib\binwalk\modules copying src\binwalk\modules\disasm.py -> build\lib\binwalk\modules copying src\binwalk\modules\entropy.py -> build\lib\binwalk\modules copying src\binwalk\modules\extractor.py -> build\lib\binwalk\modules copying src\binwalk\modules\general.py -> build\lib\binwalk\modules copying src\binwalk\modules\hexdiff.py -> build\lib\binwalk\modules copying src\binwalk\modules\signature.py -> build\lib\binwalk\modules copying src\binwalk\modules\__init__.py -> build\lib\binwalk\modules copying src\binwalk\core\common.py -> build\lib\binwalk\core copying src\binwalk\core\compat.py -> build\lib\binwalk\core copying src\binwalk\core\display.py -> build\lib\binwalk\core copying src\binwalk\core\exceptions.py -> build\lib\binwalk\core copying src\binwalk\core\idb.py -> build\lib\binwalk\core copying src\binwalk\core\magic.py -> build\lib\binwalk\core copying src\binwalk\core\module.py -> build\lib\binwalk\core copying src\binwalk\core\plugin.py -> build\lib\binwalk\core copying src\binwalk\core\settings.py -> build\lib\binwalk\core copying src\binwalk\core\statuserver.py -> build\lib\binwalk\core copying src\binwalk\core\version.py -> build\lib\binwalk\core copying src\binwalk\core\__init__.py -> build\lib\binwalk\core running build_scripts running install_lib running install_egg_info running egg_info writing src\binwalk.egg-info\PKG-INFO writing dependency_links to src\binwalk.egg-info\dependency_links.txt writing top-level names to src\binwalk.egg-info\top_level.txt reading manifest file 'src\binwalk.egg-info\SOURCES.txt' adding license file 'LICENSE' adding license file 'NOTICE.md' writing manifest file 'src\binwalk.egg-info\SOURCES.txt' removing 'C:\Users\qqian\AppData\Local\Programs\Python\Python314\Lib\site-packages\binwalk-2.3.2-py3.14.egg-info' (and everything under it) Copying src\binwalk.egg-info to C:\Users\qqian\AppData\Local\Programs\Python\Python314\Lib\site-packages\binwalk-2.3.2-py3.14.egg-info running install_scripts copying build\scripts-3.14\binwalk -> C:\Users\qqian\AppData\Local\Programs\Python\Python314\Scripts PS E:\CTF\ctftools\binwalk-2.3.2> where binwalk PS E:\CTF\ctftools\binwalk-2.3.2> python binwalk C:\Users\qqian\AppData\Local\Programs\Python\Python314\python.exe: can't open file 'E:\\CTF\\ctftools\\binwalk-2.3.2\\binwalk': [Errno 2] No such file or directory PS E:\CTF\ctftools\binwalk-2.3.2> where binwalk PS E:\CTF\ctftools\binwalk-2.3.2> python setup.py install running install C:\Users\qqian\AppData\Local\Programs\Python\Python314\Lib\site-packages\setuptools\_distutils\cmd.py:90: SetuptoolsDeprecationWarning: setup.py install is deprecated. !! ******************************************************************************** Please avoid running ``setup.py`` directly. Instead, use pypa/build, pypa/installer or other standards-based tools. This deprecation is overdue, please update your project and remove deprecated calls to avoid build errors in the future. See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details. ******************************************************************************** !! self.initialize_options() running build running build_py copying src\binwalk\__init__.py -> build\lib\binwalk copying src\binwalk\__main__.py -> build\lib\binwalk copying src\binwalk\magic\animation -> build\lib\binwalk\magic copying src\binwalk\magic\archives -> build\lib\binwalk\magic copying src\binwalk\magic\binarch -> build\lib\binwalk\magic copying src\binwalk\magic\bincast -> build\lib\binwalk\magic copying src\binwalk\magic\binwalk -> build\lib\binwalk\magic copying src\binwalk\magic\bootloaders -> build\lib\binwalk\magic copying src\binwalk\magic\code -> build\lib\binwalk\magic copying src\binwalk\magic\compressed -> build\lib\binwalk\magic copying src\binwalk\magic\console -> build\lib\binwalk\magic copying src\binwalk\magic\crypto -> build\lib\binwalk\magic copying src\binwalk\magic\ebml -> build\lib\binwalk\magic copying src\binwalk\magic\ecos -> build\lib\binwalk\magic copying src\binwalk\magic\efi -> build\lib\binwalk\magic copying src\binwalk\magic\encoding -> build\lib\binwalk\magic copying src\binwalk\magic\executables -> build\lib\binwalk\magic copying src\binwalk\magic\filesystems -> build\lib\binwalk\magic copying src\binwalk\magic\firmware -> build\lib\binwalk\magic copying src\binwalk\magic\hashing -> build\lib\binwalk\magic copying src\binwalk\magic\images -> build\lib\binwalk\magic copying src\binwalk\magic\linux -> build\lib\binwalk\magic copying src\binwalk\magic\lzma -> build\lib\binwalk\magic copying src\binwalk\magic\misc -> build\lib\binwalk\magic copying src\binwalk\magic\network -> build\lib\binwalk\magic copying src\binwalk\magic\phones -> build\lib\binwalk\magic copying src\binwalk\magic\sql -> build\lib\binwalk\magic copying src\binwalk\magic\vxworks -> build\lib\binwalk\magic copying src\binwalk\config\extract.conf -> build\lib\binwalk\config copying src\binwalk\plugins\arcadyan.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\cpio.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\dlromfsextract.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\gzipextract.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\gzipvalid.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\jffs2valid.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\lzmaextract.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\lzmavalid.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\tar.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\ubivalid.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\unjffs2.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\unpfs.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\ziphelper.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\zlibextract.py -> build\lib\binwalk\plugins copying src\binwalk\plugins\zlibvalid.py -> build\lib\binwalk\plugins copying src\binwalk\modules\compression.py -> build\lib\binwalk\modules copying src\binwalk\modules\disasm.py -> build\lib\binwalk\modules copying src\binwalk\modules\entropy.py -> build\lib\binwalk\modules copying src\binwalk\modules\extractor.py -> build\lib\binwalk\modules copying src\binwalk\modules\general.py -> build\lib\binwalk\modules copying src\binwalk\modules\hexdiff.py -> build\lib\binwalk\modules copying src\binwalk\modules\signature.py -> build\lib\binwalk\modules copying src\binwalk\modules\__init__.py -> build\lib\binwalk\modules copying src\binwalk\core\common.py -> build\lib\binwalk\core copying src\binwalk\core\compat.py -> build\lib\binwalk\core copying src\binwalk\core\display.py -> build\lib\binwalk\core copying src\binwalk\core\exceptions.py -> build\lib\binwalk\core copying src\binwalk\core\idb.py -> build\lib\binwalk\core copying src\binwalk\core\magic.py -> build\lib\binwalk\core copying src\binwalk\core\module.py -> build\lib\binwalk\core copying src\binwalk\core\plugin.py -> build\lib\binwalk\core copying src\binwalk\core\settings.py -> build\lib\binwalk\core copying src\binwalk\core\statuserver.py -> build\lib\binwalk\core copying src\binwalk\core\version.py -> build\lib\binwalk\core copying src\binwalk\core\__init__.py -> build\lib\binwalk\core running build_scripts running install_lib running install_egg_info running egg_info writing src\binwalk.egg-info\PKG-INFO writing dependency_links to src\binwalk.egg-info\dependency_links.txt writing top-level names to src\binwalk.egg-info\top_level.txt reading manifest file 'src\binwalk.egg-info\SOURCES.txt' adding license file 'LICENSE' adding license file 'NOTICE.md' writing manifest file 'src\binwalk.egg-info\SOURCES.txt' removing 'C:\Users\qqian\AppData\Local\Programs\Python\Python314\Lib\site-packages\binwalk-2.3.2-py3.14.egg-info' (and everything under it) Copying src\binwalk.egg-info to C:\Users\qqian\AppData\Local\Programs\Python\Python314\Lib\site-packages\binwalk-2.3.2-py3.14.egg-info running install_scripts copying build\scripts-3.14\binwalk -> C:\Users\qqian\AppData\Local\Programs\Python\Python314\Scripts
11-12
### 解决`setup.py install is deprecated`警告 `setup.py install` 已被弃用,建议使用 `pip` 进行安装。如果在安装 `binwalk` 时遇到此警告,可通过以下命令使用 `pip` 进行安装: ```bash pip install binwalk ``` ### 解决`where binwalk`无反应 - **Windows 系统**: - 确认 `binwalk` 是否正确安装,可尝试执行 `binwalk --version`。若提示不是内部或外部命令,说明未正确安装,需重新使用 `pip install binwalk` 进行安装。 - 若已安装,检查系统环境变量,确保 `binwalk` 所在目录已添加到 `PATH` 环境变量中。有时系统可能先找到 `binwalk` 文件夹而忽略了 `binwalk.exe`,可调整环境变量顺序,让包含 `binwalk.exe` 的路径优先查找[^1]。 - **Linux 或 macOS 系统**:应使用 `which binwalk` 来查找 `binwalk` 路径。若没反应,可能是 `binwalk` 未安装。 - 在 Debian 或 Ubuntu 系统上,使用以下命令安装: ```bash sudo apt-get update sudo apt-get install binwalk ``` - 在 CentOS 或 RHEL 系统上,使用以下命令安装: ```bash sudo yum install epel-release sudo yum install binwalk ``` - 在 macOS 上,使用 Homebrew 安装: ```bash brew install binwalk ``` ### 解决`python binwalk`提示文件不存在 `python binwalk` 这种调用方式可能并不正确,`binwalk` 通常是作为可执行命令直接使用的。如果想通过 Python 调用 `binwalk` 的功能,可在 Python 脚本中使用 `subprocess` 模块: ```python import subprocess try: result = subprocess.run(['binwalk', '--help'], capture_output=True, text=True) print(result.stdout) except FileNotFoundError: print("binwalk 未找到,请检查安装和环境变量。") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值