AcWing开放地址法和拉链法

开放地址法,把h数组全部设置为3f,然后设定null为0x3f3f3f3f,find函数设定返回值t,如果h[t]==null,那么x在h中不存在,否则为存在

#include<iostream>
#include<cstring>
#include<string>
#define LEN 100003
#define null 0x3f3f3f3f
using namespace std;
int h[3* LEN ];
int find(int x){
    int t=((x%LEN)+LEN)%LEN;
    while(h[t]!=null&&h[t]!=x){
        ++t;
        if(t==LEN){
            t=0;
        }
    }
    return t;
}

int main(){
    int N;
    cin>>N;
    memset(h,0x3f,sizeof h);
    while(N--){
        string op;
        int x;
        cin>>op>>x;
        if(op=="I"){
            h[find(x)]=x;
        }else if(op=="Q"){
            if(h[find(x)]==null) puts("No");
            else puts("Yes");
        }
    }
    return 0;
}


拉链法
在这里插入图片描述


#include<iostream>
#define LEN 100003
#include<string>
#include<cstring>
//#define null 0x3f3f3f3f
using namespace std;
int h[LEN],e[LEN],ne[LEN],idx;
void insert(int x){
    int t=((x%LEN)+LEN)%LEN;
    e[idx]=x,ne[idx]=h[t],h[t]=idx++;
}
bool find(int x){
    int t=((x%LEN)+LEN)%LEN;
    for(int i=h[t];i!=-1;i=ne[i]){
        if(e[i]==x){
            return 1;
        }
    }
    return 0;
}
int main(){
    int N;
    cin>>N;
    memset(h,-1,sizeof h);
    while(N--){
        string op;
        int x;
        cin>>op>>x;
        if(op=="I"){
            insert(x);
        }else if(op=="Q"){
            if(!find(x)) puts("No");
            else puts("Yes");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dareu_4523

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

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

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

打赏作者

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

抵扣说明:

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

余额充值