AcWing 840. 模拟散列表
原题链接
简单
#include<iostream>
#include<cstring>
using namespace std;
const int N = 100003;
int e[N],ne[N],h[N],idx;
void insert(int x) // 要插入的数
{
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;
}
int main()
{
memset(h,-1,sizeof h);
int n;
cin >> n;
for(int i = 0;i < n;i++)
{
char op;
int s;
cin >> op >> s;
if(op == 'I') insert(s);
if(op == 'Q')
{
if(find(s)) cout << "Yes" << endl;
else cout << "No" <<endl;
}
}
return 0;
}