当前位置:网站首页>哈希表 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 记录一下MySql update会锁定哪些范围的数据
- High performance erasure code coding
- In development, why do you find someone who is paid more than you but doesn't write any code?
- arcgis js 4.x 地图中加入图片
- ThreadLocal的简单理解
- Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- Sub thread get request
- Shutter encapsulated button
- Gaode map test case
猜你喜欢
Find the common ancestor of any two numbers in a binary tree
Record the range of data that MySQL update will lock
Simple use of drools decision table
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
Tas (file d'attente prioritaire)
drools中then部分的写法
BOM DOM
In development, why do you find someone who is paid more than you but doesn't write any code?
"As a junior college student, I found out how difficult it is to counter attack after graduation."
WSL 2 will not be installed yet? It's enough to read this article
随机推荐
Openssh remote enumeration username vulnerability (cve-2018-15473)
Sweetheart leader: Wang Xinling
CDH6之Sqoop添加数据库驱动
Is the neural network (pinn) with embedded physical knowledge a pit?
Find the common ancestor of any two numbers in a binary tree
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
drools中then部分的写法
drools决策表的简单使用
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
Shutter encapsulated button
FBX import under ue4/ue5 runtime
Simple use of drools decision table
drools动态增加、修改、删除规则
How to write a pleasing English mathematical paper
堆(优先级队列)
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
刷题---二叉树--2
mysql数据库基础
Record the range of data that MySQL update will lock
Go learning notes - go based interprocess communication