Pagini recente » Cod sursa (job #1258584) | Cod sursa (job #182655) | Cod sursa (job #321539) | Cod sursa (job #2964966) | Cod sursa (job #2224427)
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const string IN_FILE = "hashuri.in";
const string OUT_FILE = "hashuri.out";
class HashTable {
public:
HashTable(const int capacity = 666013) :
size(capacity),
table(vector<vector<int>>(capacity)) {}
void insert(const int value) {
if (!contains(value)) {
table[hash(value)].push_back(value);
}
}
bool contains(const int value) const {
return findValue(value) != table[hash(value)].cend();
}
void erase(const int value) {
if (contains(value)) {
table[hash(value)].erase(findValue(value));
}
}
private:
int size;
vector<vector<int>> table;
int hash(const int value) const {
return value % size;
}
vector<int>::const_iterator findValue(const int value) const {
const int h = hash(value);
return find(table[h].cbegin(), table[h].cend(), value);
}
};
int main() {
ifstream in(IN_FILE);
ofstream out(OUT_FILE);
auto table = HashTable();
int n;
in >> n;
for (int i = 0; i < n; i++) {
int type, value;
in >> type >> value;
if (type == 1) {
table.insert(value);
} else if (type == 2) {
table.erase(value);
} else {
out << (table.contains(value) ? 1 : 0) << "\n";
}
}
in.close();
out.close();
return 0;
}