Pagini recente » Cod sursa (job #1812631) | Cod sursa (job #689915) | Cod sursa (job #1491271) | Cod sursa (job #1496409) | Cod sursa (job #2714345)
#include <stdio.h>
#include <vector>
using namespace std;
class HashTable {
private:
const int TABLE_SIZE = 666013;
vector<vector<int> > table;
int hash(int x) {return x % TABLE_SIZE;}
public:
HashTable();
void insert(int x);
void remove(int x);
bool contains(int x);
};
HashTable::HashTable() {
table.resize(TABLE_SIZE);
}
void HashTable::insert(int x) {
int list_index = hash(x);
if (!contains(x)) {
table[list_index].push_back(x);
}
}
void HashTable::remove(int x) {
int list_index = hash(x);
vector<int>::iterator it;
for (it = table[list_index].begin(); it != table[list_index].end(); ++it) {
if (*it == x) {
break;
}
}
if (it != table[list_index].end()) {
table[list_index].erase(it);
}
}
bool HashTable::contains(int x) {
int list_index = hash(x);
for (int value: table[list_index]) {
if (value == x) {
return true;
}
}
return false;
}
int main() {
FILE * fin, * fout;
int x, op, n;
fin = fopen("hashuri.in", "r");
fout = fopen("hashuri.out", "w");
fscanf(fin, "%d", &n);
HashTable hashTable;
for (int i = 0; i < n; ++i) {
fscanf(fin, "%d%d", &op, &x);
if (op == 1) {
hashTable.insert(x);
} else if (op == 2) {
hashTable.remove(x);
} else {
fprintf(fout, "%d\n", hashTable.contains(x));
}
}
fclose(fin);
fclose(fout);
return 0;
}