Pagini recente » Cod sursa (job #2454747) | Cod sursa (job #2596052) | Cod sursa (job #279764) | Cod sursa (job #1260648) | Cod sursa (job #3129718)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
template<const int P>
class HashTable {
vector<int> content;
vector<int> next;
vector<int> start;
public:
HashTable() {
start.resize(P);
next.resize(1);
content.resize(1);
}
void insert(int x) {
int hash = x % P;
content.push_back(x);
next.push_back(start[hash]);
start[hash] = content.size() - 1;
}
bool find(int x) {
int hash = x % P;
for (int i = start[hash]; i != 0; i = next[i]) {
if (content[i] == x) {
return true;
}
}
return false;
}
void erase(int x) {
int hash = x % P;
int i = start[hash];
if (content[i] == x) {
start[hash] = next[i];
} else {
while (next[i] != 0 && content[next[i]] != x) {
i = next[i];
}
if (content[next[i]] == x) {
next[i] = next[next[i]];
}
}
}
};
int main() {
ifstream cin("hashuri.in");
ofstream cout("hashuri.out");
HashTable<666013> set;
int n; cin >> n;
for (int i = 0; i < n; i++) {
int op, x; cin >> op >> x;
if (op == 1) {
set.insert(x);
} else if (op == 2) {
set.erase(x);
} else {
cout << set.find(x) << "\n";
}
}
return 0;
}