哈希表(Hash)
作者:
烟雨宴江南
,
2024-07-30 18:10:36
,
所有人可见
,
阅读 1
//拉链法
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100010;
int h[N], e[N], ne[N], idx = 0;
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;
while(n--)
{
char c;
cin >> c;
if(c == 'I')
{
int x;
cin >> x;
insert(x);
}
if(c == 'Q')
{
int x;
cin >> x;
if(find(x)) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
return 0;
}
//