当前位置:网站首页>哈希表 AcWing 840. 模拟散列表
哈希表 AcWing 840. 模拟散列表
2022-07-02 09:43:00 【T_Y_F666】
哈希表 AcWing 840. 模拟散列表
原题链接
算法标签
哈希表
思路
拉链法
开放寻址法
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 100003;
int a[N], e[N], h[N], ne[N], idx;
int s;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
// 采用头插法 将数据插入对应hash值为k的hash表中
void insert(int x){
//保证负数也可以映射到hash表中 所模数字为质数 可以尽量减少冲突
int k=(x%N+N)%N;
e[idx]=x;
ne[idx]=h[k];
h[k]=idx++;
}
bool find(int x){
int k=(x%N+N)%N;
for(int i=h[k]; i!=-1; i=ne[i]){
if(e[i]==x){
return true;
}
}
return false;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(h, -1, sizeof h);
int n=read();
while(n--){
char op[2];
scanf("%s", op);
int x=read();
if(op[0]=='I'){
insert(x);
}else{
if(find(x)){
puts("Yes");
}else{
puts("No");
}
}
}
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Shutter encapsulated button
- How to write a pleasing English mathematical paper
- LeetCode—<动态规划专项>剑指 Offer 19、49、60
- CPU指令集介绍
- CDH6之Sqoop添加数据库驱动
- CDA data analysis -- common knowledge points induction of Excel data processing
- (C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
- 深拷貝 事件總線
- Post request body content cannot be retrieved repeatedly
- BOM DOM
猜你喜欢
Find the common ancestor of any two numbers in a binary tree
JSON序列化 与 解析
Enhance network security of kubernetes with cilium
Heap (priority queue)
线性DP AcWing 899. 编辑距离
中国交通标志检测数据集
Deep understanding of P-R curve, ROC and AUC
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
Drools dynamically add, modify, and delete rules
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
随机推荐
Fluent fluent library encapsulation
Performance tuning project case
线性DP AcWing 899. 编辑距离
Leetcode739 daily temperature
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
LeetCode—剑指 Offer 51. 数组中的逆序对
分布式机器学习框架与高维实时推荐系统
drools中then部分的写法
Deep copy event bus
drools执行指定的规则
上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
Addition, deletion, modification and query of MySQL table (Advanced)
Shuttle encapsulated AppBar
Tas (file d'attente prioritaire)
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
计算二叉树的最大路径和
染色法判定二分图 AcWing 860. 染色法判定二分图
Test shift left and right
堆(優先級隊列)