Pagini recente » Cod sursa (job #2747976) | Cod sursa (job #422253) | Cod sursa (job #143917) | Cod sursa (job #2307284) | Cod sursa (job #2035055)
#define __HASHURI__
#ifdef __HASHURI__
#include <fstream>
using namespace std;
class LinkedList {
private:
struct Node {
int value;
Node *next;
};
Node *root;
public:
LinkedList() {
root = new Node();
root->next = nullptr;
}
void add(int n) {
Node *newNode = new Node();
newNode->value = n;
newNode->next = root->next;
root->next = newNode;
}
void remove(int n) {
Node *it = root;
while (it->next != nullptr) {
if (it->next->value == n) {
Node *secondNext = it->next->next;
delete it->next;
it->next = secondNext;
return;
}
it = it->next;
}
}
bool has_item(int n) {
Node *it = root->next;
while (it != nullptr) {
if (it->value == n) {
return true;
}
it = it->next;
}
return false;
}
};
class HashMap {
private:
int size;
LinkedList *buckets;
public:
explicit HashMap(int size) : size(size) {
buckets = new LinkedList[size];
}
void add(int x) {
int bucket = x % size;
buckets[bucket].add(x);
}
void remove(int x) {
int bucket = x % size;
buckets[bucket].remove(x);
}
bool has_item(int x) {
int bucket = x % size;
return buckets[bucket].has_item(x);
}
};
#include <assert.h>
void sanity_check() {
LinkedList l;
assert(!l.has_item(10));
l.add(10);
l.add(20);
l.add(-6);
assert(l.has_item(10));
assert(l.has_item(20));
assert(l.has_item(-6));
l.remove(10);
assert(!l.has_item(10));
}
int main() {
// sanity_check();
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
int n;
fin >> n;
HashMap hashMap(100000);
for (int i = 0; i < n; ++i) {
int op, arg;
fin >> op >> arg;
switch (op) {
case 1:
hashMap.add(arg);
break;
case 2:
hashMap.remove(arg);
break;
case 3:
fout << (hashMap.has_item(arg) ? 1 : 0) << "\n";
break;
default:
break;
}
}
return 0;
}
#endif