这里有开放寻址法和拉链法,大家可以根据自己的喜好去读,第一个是拉链法,第二个是开放寻址法。
#include<cstring>
#include<iostream>
using namespace std;
int h[100003],e[100003],ne[100003],idx;
void insert(int x)
{
int k=(x%100003+100003)%100003;
e[idx]=x,ne[idx]=h[k],h[k]=idx++;
}
bool find(int x)
{
int i,k=(x%100003+100003)%100003;
for(i=h[k];i!=-1;i=ne[i])if(e[i]==x)return true;
return false;
}
int main()
{
int n,x;
char op[2];
scanf("%d",&n);
memset(h,-1,sizeof(h));
while(n--){
scanf("%s%d",op,&x);
if(*op=='I')insert(x);
else{
if(find(x))printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
#include<cstring>
#include<iostream>
using namespace std;
const int BIG=1061109567;
int h[200003];
int find(int x)
{
int t=(x%200003+200003)%200003;
while(h[t]!=BIG&&h[t]!=x){
t++;
if(t==200003)t=0;
}
return t;
}
int main()
{
int n,x;
char op[2];
memset(h,0x3f,sizeof h);
scanf("%d",&n);
while(n--){
scanf("%s%d",op,&x);
if (*op=='I')h[find(x)]=x;
else{
if(h[find(x)]==BIG)printf("No\n");
else printf("Yes\n");
}
}
return 0;
}