hash:
1.插入整数(范围大,如-1e9-1e9),2.查询某整数是否出现过
1.拉链法:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
#define LL long long
using ull = unsigned long long;
const LL N = 1e5 + 3;//N最好是质数
vector<int> p[N];
void insert(int x)
{
int k = (x % N + N) % N;
p[k].push_back(x);
}
int find(int x)
{
int k = (x % N + N) % N;
for (int i = 0; i < p[k].size(); i++)
{
if (p[k][i] == x)
return 1;
}
return 0;
}
int main() {
int n,x;
char op[2];
cin >> n;
for (int i = 1; i <= n; i++)
{
scanf("%s %d", op, &x);
if (op[0] == 'I')
{
insert(x);
}
else
{
if (find(x))
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
2.开放寻地址
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
#define LL long long
using ull = unsigned long long;
const LL N = 2e5 + 3;//N最好是质数并且是操作数的2-3倍
int p[N];//p存映射前整数值
int find(int x)
{
int k = (x % N + N) % N;
while (p[k] !=1e9+10 && p[k] != x)
{
k++;
if (k == N)
k = 0;
}
return k;
}
int main() {
int n,x;
char op[2];
cin >> n;
for (int i = 0; i <= N; i++)
p[i] = 1e9 + 10;
for (int i = 1; i <= n; i++)
{
scanf("%s %d", op, &x);
int k = find(x);
if (op[0] == 'I')
p[k] = x;
else
{
if (p[k]!=1e9+10)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}