当前位置:网站首页>哈希表 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- [I'm a mound pytorch tutorial] learning notes
- CDA data analysis -- Introduction and use of aarrr growth model
- High performance erasure code coding
- Drools executes the specified rule
- Anti shake throttle
- LeetCode—剑指 Offer 59 - I、59 - II
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
- Distributed machine learning framework and high-dimensional real-time recommendation system
- MySQL indexes and transactions
猜你喜欢

CDH6之Sqoop添加数据库驱动

Deep understanding of P-R curve, ROC and AUC

AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning

Jenkins voucher management

The blink code based on Arduino and esp8266 runs successfully (including error analysis)

计数类DP AcWing 900. 整数划分

The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night

arcgis js 4.x 地图中加入图片

Go learning notes - multithreading

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
随机推荐
包管理工具
趣味 面试题
drools执行指定的规则
Adding database driver to sqoop of cdh6
Sparkcontext: error initializing sparkcontext solution
CDH6之Sqoop添加数据库驱动
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
浏览器存储方案
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
Anti shake throttle
甜心教主:王心凌
线性DP AcWing 895. 最长上升子序列
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
Lombok common annotations
Drools executes string rules or executes a rule file
Gaode map test case
Leetcode - Sword finger offer 51 Reverse pairs in an array
Fluent fluent library encapsulation
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间