维护一个集合,支持如下几种操作:
I x
,插入一个数 x;Q x
,询问数 x 是否在集合中出现过;
现在要进行 N 次操作,对于每个询问操作输出对应的结果。
输入格式
第一行包含整数 N,表示操作数量。
接下来 N 行,每行包含一个操作指令,操作指令为 I x
,Q x
中的一种。
输出格式
对于每个询问指令 Q x
,输出一个询问结果,如果 x 在集合中出现过,则输出 Yes
,否则输出 No
。
每个结果占一行。
数据范围
1≤N≤1e5
−1e9≤x≤1e9
输入样例:
5
I 1
I 2
I 3
Q 2
Q 5
输出样例:
Yes
No
Code 1: 拉链法
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
const int N = 100003;
int h[N], e[N], ne[N], idx;
string op;
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(x == e[i]) return true;
}
return false;
}
signed main()
{
int n;
scanf("%d", &n);
memset(h, -1, sizeof h);
int x;
for(int i = 1; i <= n; i ++ )
{
cin >> op >> x;
if(op == "I") Insert(x);
else
{
if(find(x)) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
}
Code 2: 开放寻址法
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
const int N = 200003, null = 0x3f3f3f3f;
int h[N];
int find(int x)
{
int k = (x % N + N) % N;
while(h[k] != null && h[k] != x)
{
k ++;
if(k == N) k = 0;
}
return k;
}
signed main()
{
int n, x;
scanf("%d", &n);
memset(h, 0x3f3f3f3f, sizeof h);
for(int i = 1; i <= n; i ++ )
{
string op;
cin >> op >> x;
int k = find(x);
if(op == "I") h[k] = x;
else
{
if(h[k] != null) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
}