遇到具体问题时,hash函数取模的N为比数据范围大的最小的质数就行,可以提前打表求出。
#include<iostream>
#include<cstring>
using namespace std;
const int N=100003;
int n;
int h[N],e[N],ne[N],idx;
int hs(int x){
return ((x%N)+N)%N;
}
bool insert(int x){
int t=hs(x);
e[idx]=x,ne[idx]=h[t],h[t]=idx++;
}
int find(int x){
int t=hs(x);
for(int i=h[t];i!=-1;i=ne[i]){
if(e[i]==x)return true;
}
return false;
}
int main(){
cin>>n;
memset(h,-1,sizeof h);
char op;
int x;
while(n--){
cin>>op>>x;
if(op=='I')
insert(x);
else{
if(find(x))cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
}