1004. Hashing

C++项目集成Bcrypt密码加密:从原理到Visual Studio实战 密码安全是系统安全的基石,其核心在于如何安全地存储用户凭证。传统哈希算法如MD5和SHA-1因计算速度过快,易受彩虹表攻击和暴力破解,已无法满足现代安全需求。现代密码存储的核心原理是采用**慢哈希**和**加盐**机制,通过增加计算成本来抵御攻击。Bcrypt算法正是这一理念的杰出代表,它通过内置盐值和可调节的成本因子,实现了算法自适应性,让防御方能够跟上硬件算力的发展。在工程实践中,尤其是在C++后台服务或桌面应用开发中,正确集成Bcrypt至关重要。本文将以Visual Studio开发环境为例,详细解 阅读详情
Description

Implement the following Hash class using separate chaining.

template <typename HashedObj>
class HashTable
{
  public:
     HashTable(int (* h)(HashedObj x), int size = 101 )
      : currentSize( 0 ), hash(h)
      { theLists.resize( size ); }

    bool contains( const HashedObj & x ) const

// If x is in the table, it returns true, otherwise, it returns false.
    {
//insert here.        

    }

    void makeEmpty( )
    {
        for( int i = 0; i < theLists.size( ); i++ )
            theLists[ i ].clear( );
    }

    bool insert( const HashedObj & x )

//Insert key x  if x is not in the table and return true. Otherwise, return false and do nothing.  If the table is full, you will call rehash.
    {
//insert here.                   

          }

    bool remove( const HashedObj & x )

//If x is present in the table, remove it and return true. Otherwise, return false. 
    {
//insert here.        

    }

  private:
    vector<list<HashedObj> > theLists;   // The array of Lists
    int  currentSize;
    int (*hash)(HashedObj x); //hash function

        int myhash( const HashedObj & x ) const
    {
        int hashVal = hash( x );

        hashVal %= theLists.size( );
        if( hashVal < 0 )
            hashVal += theLists.size( );

        return hashVal;
    }

 void rehash( )
    {
        vector<list<HashedObj> > oldLists = theLists;

            // Create new double-sized, empty table
        theLists.resize( nextPrime( 2 * theLists.size( ) ) );
        for( int j = 0; j < theLists.size( ); j++ )
            theLists[ j ].clear( );

            // Copy table over
        currentSize = 0;
        for( int i = 0; i < oldLists.size( ); i++ )
        {
            list<HashedObj>::iterator itr = oldLists[ i ].begin( );
            while( itr != oldLists[ i ].end( ) )
                insert( *itr++ );
        }
    }
};

 
 
Hint

The constructor HashTable has two parameters: the first one specifies your hash function and the second one specifies the size of the hash table. 

For example, you may create a hash table like the following:

HashTable t(h, 10007);

where h is a hash function you have defined.  

You may use the following functions:

 

bool isPrime( int n )
{
    if( n == 2 || n == 3 )
        return true;

    if( n == 1 || n % 2 == 0 )
        return false;

    for( int i = 3; i * i <= n; i += 2 )
        if( n % i == 0 )
            return false;

    return true;
}

int nextPrime( int n )
{
    if( n % 2 == 0 )
        n++;

    for( ; !isPrime( n ); n += 2 )
        ;

    return n;
}


题目要求实现一个哈希类。此处需要注意的是:

1、在类的成员函数后加const,就表明这个函数是不能改变类的成员变量,相当于给this指针加上了const修饰,也不能调用其他非const成员函数,否则编译器会报错;所以在调用与成员变量有关的迭代器时,迭代器类型是const;

2、容器中的类型为模板类型时,在使用迭代器是也要在前面加上 typename;

3、函数指针:是指向函数的指针变量,有了指向函数的指针变量后,可用该指针变量调用函数,函数指针有两个用途:调用函数和做函数的参数;

4、标准库中的list容器有push_back, push_front, pop_back, pop_front;


#include <iostream>
#include <vector>
#include <list>
using namespace std;

bool isPrime( int n )
{
    if( n == 2 || n == 3 )
        return true;
    
    if( n == 1 || n % 2 == 0 )
        return false;
    
    for( int i = 3; i * i <= n; i += 2 )
        if( n % i == 0 )
            return false;
    
    return true;
}

int nextPrime( int n )
{
    if( n % 2 == 0 )
        n++;
    
    for( ; !isPrime( n ); n += 2 )
        ;
    
    return n;
}

template <typename HashedObj>
class HashTable
{
public:
    HashTable(int (* h)(HashedObj x), int size = 101 )
    : currentSize( 0 ), hash(h)
    { theLists.resize( size ); }
    
    //任何不会修改数据成员的函数都应该声明为const类型。如果在编写const成员函数时,不慎修改了数据成员,或者调用了其它非const成员函数,编译器将指出错误,这无疑会提高程序的健壮性,相当于给this指针加上了const修饰。
    bool contains( const HashedObj & x ) const
    
    // If x is in the table, it returns true, otherwise, it returns false.
    {
        int key = myhash(x);
        //模板类型的迭代器,声明时前面要加“typename”,注意迭代器类型是const
        typename list<HashedObj>::const_iterator it = theLists[key].begin();
        while (it != theLists[key].end()) {
            if (x == *it) {
                return true;
            }
            it++;
        }
        return false;
    }
    
    void makeEmpty()
    {
        for( int i = 0; i < theLists.size( ); i++ )
            theLists[ i ].clear( );
    }
    
    bool insert( const HashedObj & x )
    
    //Insert key x  if x is not in the table and return true. Otherwise, return false and do nothing.  If the table is full, you will call rehash.
    {
        if (contains(x)) {
            return false;
        }
        if (currentSize + 1 > theLists.size()) {
            rehash();
        }
        int key = myhash(x);
        theLists[key].push_back(x);
        currentSize++;
        return true;
    }
    
    bool remove( const HashedObj & x )
    
    //If x is present in the table, remove it and return true. Otherwise, return false.
    {
        if (!contains(x)) {
            return false;
        }
        int key = myhash(x);
        typename list<HashedObj>::iterator it = theLists[key].begin();
        while (it != theLists[key].end()) {
            if (x == *it) {
                theLists[key].erase(it);
                return true;
            }
            it++;
        }
    }
    
private:
    vector<list<HashedObj> > theLists;   // The array of Lists
    int  currentSize;
    int (*hash)(HashedObj x); //hash function
    
    int myhash( const HashedObj & x ) const
    {
        int hashVal = hash( x );
        
        hashVal %= theLists.size( );
        if( hashVal < 0 )
            hashVal += theLists.size( );
        
        return hashVal;
    }
    
    void rehash( )
    {
        vector<list<HashedObj> > oldLists = theLists;
        
        // Create new double-sized, empty table
        theLists.resize( nextPrime( 2 * theLists.size( ) ) );
        for( int j = 0; j < theLists.size( ); j++ )
            theLists[ j ].clear( );
        
        // Copy table over
        currentSize = 0;
        for( int i = 0; i < oldLists.size( ); i++ )
        {
            typename list<HashedObj>::iterator itr = oldLists[i].begin();
            while( itr != oldLists[ i ].end( ) )
                insert( *itr++ );
        }
    }
};



大数据环境下高效数据查找与对象匹配技术方案全解析 在大数据处理中,数据查询与关联是核心基础操作,其本质是在海量数据集中实现高效、精准的定位与匹配。从技术原理上看,这通常涉及分布式计算框架对数据的并行处理,以及各类匹配算法对数据关系的挖掘。其技术价值在于能够支撑用户画像、商品推荐、风险识别等关键业务场景,实现从海量信息中快速提取价值。本文聚焦于大数据环境下的数据匹配问题,通过介绍精确匹配、模糊匹配、关联关系挖掘和实时查找等典型任务,结合Spark、Flink等主流工具,深入探讨了从基础概念到生产实践的完整解决方案,并特别关注了数据倾斜、性能调优等工程实践中的 阅读详情

相关推荐

CTF密码学入门:从Base64到XOR的签到题实战解析

密码学是网络安全与数据保护的基石,其核心原理在于通过编码与加密算法实现信息的保密性、完整性和认证。在工程实践中,Base64、Hex等编码技术常用于数据的安全传输与存储,而古典密码(如凯撒密码)和现代加密算法(如XOR、AES)则构成了从简单替换到复杂加密的技术谱系。这些基础技术广泛应用于CTF竞赛、安全审计和渗透测试等场景,帮助从业者快速识别与处理常见的数据伪装手段。本文聚焦于CTF中的“cry签到题”,通过解析Base64解码、ROT13位移及XOR异或等高频考点,结合Python脚本与CyberChe

tang_zongyun的博客 491

RDMA多路径与负载均衡策略深度解析:ECMP、自适应路由与包级喷洒(AI智算必知必会)

本文深度解析RDMA多路径与负载均衡策略,从芯片验证视角剖析ECMP哈希极化、自适应路由(ARS)及包级喷洒(MRC/DDP)的硬件实现。涵盖IPv6 Flow Label熵源注入、RTL级数据流时序、PCIe BAR映射及WQE多路径调度机制,结合H3C与NVIDIA实战配置与尾延迟评测,为万卡AI智算网络底座提供芯片级调优指南。

专注分享DPU、RDMA智能网卡/SSD(固态硬盘)产品测试与优化 815

【信息科学与工程学】【安全领域】第三十五篇 网络安全算法表

编号算法名称算法的逐步骤思考推理的数学方程式和分解的数学方程式参数/变量列表复杂度(时间/空间)1​AES (高级加密标准)​1. 密钥扩展:从初始密钥生成轮密钥。 2. 初始轮: 3. 主轮循环(Nr-1轮):  - SubBytes:(非线性字节替换)  - ShiftRows:对状态矩阵的行进行循环移位。  - MixColumns:(在GF(2⁸)上的矩阵乘法)  - AddRoundKey: 4. 最终轮:执行SubBytes、ShiftRows、AddR

weixin_49199313的博客 1379

1001. Bacon Number

Description The Bacon number of an actor is the number of degrees of separation he or she has from Bacon. For example, Tom Hanks has a Bacon number 1, because he was in Apollo 13 with Kevin Bacon.

sss_coder的专栏 798

【信息科学与工程学】【通信工程】第一百九十一篇 路由器体系结构03

编号类型领域问题详细的数学分析(逐步推理)参数列表(表达式 / 数值范围 / 边界 / 测量)关联知识839DCI演进 · 收敛DCI 2030 年三大技术收敛方向步骤1:方向一:IPoDWDM 成为绝对主流(Omdia 2027 59% → 2030 80%+),OTN 退化为 OLS 纯光层。步骤2:方向二:800ZR+ 取代 400ZR 成为主力速率,PCS 使 800G 达 600–1000km。步骤3:方向三:AI 集群 DCI 催生 1.6T/3.2T 超短距(<10km)相干模块(OSFP-X

weixin_49199313的博客 862

【信息科学与工程学】【通信工程】第八十六篇 通信网络设备及通信网络组网的所有学科知识01

编号001类型网络虚拟化与覆盖技术网络领域数据中心网络子领域大二层网络 / 网络虚拟化核心数学方程式/算法模型核心算法:VXLAN封装与解封装。关键在于24位VNI(虚拟网络标识符)到物理网络的三层IP地址的映射。封装:收到原始以太帧后,为其添加 VXLAN 头部(含 VNI)、外层 UDP 头(目的端口 4789)、外层 IP 头(源 IP 为本地 VTEP IP,目的 IP 为对端 VTEP IP)以及外层 MAC 头。解封装:对端 收到报文后,剥离外层头部,根据内层帧的目的 MAC 和 VNI 进行二

weixin_49199313的博客 842

1003. Width of Binary Trees

Description The maximum number of nodes in all levels in a binary tree is called the width of the tree. For example, the binary tree on the right in the following figure has width 4 because the max

sss_coder的专栏 706

【信息科学与工程学】【数据科学】数据科学领域——第三篇 数学08 几何学00总篇

编号英文名称中文名称简要说明/归属1Foundations of Geometry几何基础公理化方法, Hilbert《几何基础》2Euclidean Geometry (Synthetic)欧几里得几何(综合法)《几何原本》传统3Analytic Geometry解析几何坐标方法, Descartes, Fermat4Plane Geometry平面几何三角形、圆、共点共线等经典问题5Solid Geometry立体几何三维空间中的图形与度量6Non-Euclidean Geometry非欧几何平行公设不

weixin_49199313的博客 203

1005. 业绩排名

Description 年末公司将职员按照业绩从大到小排序后分配奖金。假定输入是职员姓名以及业绩, 业绩是正整数。 请你按照职员业绩从高到低将职员排序,业绩相同时按照输入先后次序排序。 输出是排序后的名次和职员姓名列表, 业绩相同时,名次相同。 例如,输入: (wang ming, 320), (gao min, 330), (zhang li, 280), (luo yan, 320

sss_coder的专栏 644

【信息科学与工程学】计算机科学与技术——第七十七篇 系统架构设计02

编号类型模型配方企业资本运作的方法/规则/条件/依据及所有的步骤(​风险处理/异常处理/危机应对关联知识法律法规和裁决依据​​由发起人(通常是知名投资人、投行或产业资本)先设立一个只有现金、无实际业务的特殊目的收购公司(SPAC),通过IPO募集资金并在交易所上市。然后在规定时间内(通常24个月)寻找一家非上市公司进行合并(De-SPAC),使目标公司迅速实现上市,规避传统IPO的复杂流程和不确定性。1.:在开曼或特拉华注册空壳公司,注入少量种子资金。2.

weixin_49199313的博客 315

097634e9169c6777120550ad61afb6bc-1.jpg

097634e9169c6777120550ad61afb6bc-1.jpg

1010. bracket matching

Description Implement the following function that decides if a list of brackets match. bool brackets(string s); /* The input s is a list of brackets, including {,},(,),[,], and possibly blanks.

sss_coder的专栏 630

Kali Linux实战:Aircrack-ng破解Wi-Fi密码的完整流程与常见问题解决

本文详细介绍了在Kali Linux中使用Aircrack-ng套件进行Wi-Fi密码破解的完整流程,包括环境准备、无线网卡配置、网络扫描、握手包捕获与字典破解等关键步骤。同时针对无线网卡不支持监听模式、无法捕获握手包等常见问题提供了实用解决方案,帮助安全研究人员有效评估无线网络安全性。

weixin_29323049的博客 297

1011. Find the external path length

Description Given the linked representation of binary trees typedef int T; struct BinaryNode{ T data; //store data  BinaryNode *left, *right; BinaryNode(T d, BinaryNode *l=NULL, BinaryNode*

sss_coder的专栏 626

嵌入式存储进阶:除了KV存储,EasyFlash的IAP和LOG功能怎么玩?

本文深入探讨了嵌入式存储方案EasyFlash的高级功能,包括在线固件升级(IAP)和闪存日志存储(LOG)。通过详细解析IAP的双区备份设计和LOG的环形缓冲区架构,提供硬件适配、安全升级流程和日志管理技巧等实战指南,帮助开发者在物联网设备中实现可靠的数据存储和固件更新。

weixin_29280219的博客 414

使用Docker搭建SearXNG搜索引擎

【代码】使用Docker搭建SearXNG搜索引擎。

janthinasnail的博客 1168

1007. Find the middle element in a Linked List

Description Assuming the following definition: template struct Node {    Node_entry entry;    Node *next;    Node();    Node(Node_entry item, Node *add_on = NULL); }; template Node::Node(

sss_coder的专栏 617

深度学习12—VGG19实现

state_dict = load_state_dict_from_url(model_urls['vgg19'],model_dir='./model' ,progress=progress)#预训练模型地址。accuracy=((out.argmax(1)==label).sum()).clone().detach().cpu().numpy()#正确预测的总和比测试集的长度,即预测正确的精度。image=torch.reshape(image,(1,3,224,224))#四维图形,RGB三个通。

m0_55196097的博客 2444

1000. Minimum Weight

Description Given a list of weights w1, w2, ..., wn, we can construct a binary tree with n leaves, where each leaf is marked with one weight. The is called a weighted binary tree. The weight of t

sss_coder的专栏 528

基于Joplin和Joplin Server搭建私有云笔记

我们知道,多端同步功能是笔记工具最重要的功能。只有实现了多端同步,我们才能在工作电脑和手机之间无缝切换笔记体验。Joplin 在同步上做得不错,支持 Dropbox、OneDrive 和 AWS s3(当然国内都没法用),支持 WebDAV 协议,也支持自家的 Joplin Server。 本文介绍如何在自己的服务器上搭建 Joplin Server,并配置好 Joplin Desktop 的同步功能。 前置条件 安装docker 创建 Joplin Server 配置文件 创建配置文件,~/docke

代码时光 1万+

AI上推荐 之 隐语义模型(LFM)和矩阵分解(MF)

1. 前言 随着信息技术和互联网的发展, 我们已经步入了一个信息过载的时代,这个时代,无论是信息消费者还是信息生产者都遇到了很大的挑战: 信息消费者:如何从大量的信息中找到自己感兴趣的信息? 信息生产者:如何让自己生产的信息脱颖而出, 受到广大用户的关注? 为了解决这个矛盾, 推荐系统应时而生, 并飞速前进,在用户和信息之间架起了一道桥梁,一方面帮助用户发现对自己有价值的信息, 一方面让信息能够展现在对它感兴趣的用户前面。 推荐系统近几年有了深度学习的助推发展之势迅猛, 从前深度学习的传统推荐模型(协

Miracle8070 1万+

1002. Is It Acyclic

Description Given a directed graph, decide if it is acyclic (there is no directed cycle). Input There are multiple test cases. The first line contains an integer T, indicating the number of

sss_coder的专栏 400

5、卡尔ibration中Denavit - Hartenberg与Hayati约定的转换

本文详细介绍了机器人运动学中Denavit-Hartenberg(DH)参数与Hayati参数的定义、适用场景及其相互转换方法。重点分析了两种参数在描述关节坐标系变换时的差异,特别是在处理平行或接近平行关节轴时的稳定性问题,并给出了从DH到Hayati及反向转换的具体数学公式与流程图。通过参数转换,可在不改变运动学模型的前提下提升建模精度与计算鲁棒性,适用于机器人标定与运动学分析等应用场景。

h0i1j2k3l的博客 121

1006. Join two lists

Description list join(const  list &a, const list &b) //returns a list of distinct integers both in a and b. For example, a = (3,4,3,5); b = (2,3,2,12,4,4), then c = (3,4). or c = (4,3).  The ord

sss_coder的专栏 361

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

上一篇: 1003. Width of Binary Trees
下一篇: 1005. 业绩排名
Eden_LZH
博客等级 码龄12年 1粉丝 13原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值