Cod sursa(job #2035060)

Utilizator alexpanaAlexandru Pana alexpana Data 8 octombrie 2017 21:32:06
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.17 kb
#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 <map>

int main() {
    ifstream fin("hashuri.in");
    ofstream fout("hashuri.out");
    map<int, bool> hash_map;
    int n;
    fin >> n;
    HashMap hashMap(100000);
    for (int i = 0; i < n; ++i) {
        int op, arg;
        fin >> op >> arg;
        switch (op) {
            case 1:
                hash_map[arg] = true;
                break;
            case 2:
                hash_map[arg] = false;
                break;
            case 3:
                fout << (hash_map[arg] ? 1 : 0) << "\n";
                break;
            default:
                break;
        }
    }
    return 0;
}

#endif