c++实现哈希查找算法
时间: 2023-11-30 14:42:38 浏览: 111
以下是C++实现哈希查找算法的示例代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100010;
int h[N], e[N], ne[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;
while (n--)
{
char op;
int x;
cin >> op >> x;
if (op == 'I') insert(x);
else
{
if (find(x)) puts("Yes");
else puts("No");
}
}
return 0;
}
```
阅读全文