数据结构——哈希表

哈希表存储结构

在这里插入图片描述

哈 希 表 存 储 结 构 { 开 放 寻 址 法 拉 链 法   哈希表存储结构\left\{\begin{matrix} 开放寻址法\\拉链法\ \end{matrix}\right. { 

注意:当构造哈希函数构造成 mod a 形式的话, 这个mod的数,尽可能取成质数,并且,尽可能的距离2的幂次方远(在数学上可以证明,这么取,冲突的概率是最小的)
在这里插入图片描述
在这里插入图片描述

#include<iostream>
using namespace std;
//这个题N是100000,所以哈希函数模的那个数找个比100000大的质数
const int N = 100003;


//链表寻址法
int head[N],e[N],ne[N],idx;  //每个节点开个链表,head数组存储头结点位置

int the_hash(int x)
{
    return (x % N + N) % N;   //这样主要是防止如果x是负数,仅仅x%N模也是个负数
}

void insert(int x)
{
    int k = the_hash(x);
    e[idx] = x;
    ne[idx] = head[k];
    head[k] = idx;
    idx++;
}

bool query(int x)
{
    int k = the_hash(x);
    bool flag = false;
    for(int i = head[k];i!=-1;i=ne[i])
    {
        if(e[i]==x){
            flag = true;
            break;
        }
    }
    return flag;
}

int main()
{
    for(int i=0;i<N;i++) head[i] = -1;
    
    int m;
    cin >> m;
    
    while(m--)
    {
        int x;
        string op;
        cin >> op >> x;
        if(op=="I") insert(x);
        if(op=="Q"){
            if(query(x)) puts("Yes");
            else puts("No");
        }
    }
    return 0;
}

字符串哈希(字符串前缀哈希法)

在这里插入图片描述
在这里插入图片描述

#include<iostream>
using namespace std;

typedef unsigned long long ULL;
const int N = 100010;
const int p = 131; //p进制,131是个经验值,选择131或者13331这样从经验上看,有冲突的可能最小,基本上没冲突

ULL the_hash[N];  //存储一个字符串n个前缀的hash值
//就比如说str="ABCD" , str[0]=0; str[1]='A'*p^0;  str[2] = 'A'*p^1 + 'B'*p^0
//先做个预处理,把一个字符串的前缀都对应到the_hash表中

ULL the_p[N];  //存储p的几次方

bool query(int l1,int r1,int l2,int r2)
{
    ULL hash1 = the_hash[r1] - the_hash[l1-1]*the_p[r1-l1+1];
    ULL hash2 = the_hash[r2] - the_hash[l2-1]*the_p[r2-l2+1];
    if(hash1==hash2) return true;
    else return false;
}


int main()
{
    int n,m;   //n代表字符串的长度
    cin >> n >> m; 
    string a,str;
    cin >> a;
    str = "0" + a;

    //先把p的几次方预处理下
    the_p[0] = 1;
    for(int i=1;i<=n;i++)
    {
        the_p[i] = the_p[i-1]*p;
    }
    
    //计算字符串的前缀hash表
    the_hash[0] = 0;
    for(int i=1;i<=n;i++)
    {
        the_hash[i] = the_hash[i-1]*p + str[i];
    }
    
    while(m--)
    {
        int l1,r1,l2,r2;
        cin >> l1 >> r1 >> l2 >> r2;
        if(query(l1,r1,l2,r2)) puts("Yes");
        else puts("No");
    }
    
    return 0;
}

例题:2018年全国硕士研究生招生考试数据结构大题

在这里插入图片描述

class Solution {
public:
    int findMissMin(vector<int>& nums) {
        int n = nums.size();
        bool hash[n+2];
        for(int i=0;i<n;i++)
        {
            if(nums[i]>=1 && nums[i]<=n) hash[nums[i]] = true;
            //因为输入n个数,最大结果可能为n+1那么,即是输入数组有大于n的数,那么肯定是某个小于n
            //的数没有在这个数组里才会这样
        }
        for(int i=1;i<=n;i++){
            if(hash[i]==false) return i; 
        }
        return n+1;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新城里的旧少年^_^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值