Cod sursa(job #3130239)

Utilizator omaclearuMacelaru Octavian Andrei omaclearu Data 17 mai 2023 10:09:16
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

class HashTable {
private:
    static const int MOD = 666013;
    vector<vector<int>> table;
    int n;

    bool search(int x) {
        int index = x % MOD;
        const vector<int> &bucket = table[index];

        for (auto value: bucket) {
            if (value == x) {
                return true;
            }
        }
        return false;
    }

    void insert(int x) {
        if (!search(x)) {
            int index = x % MOD;
            table[index].push_back(x);
        }
    }

    void remove(int x) {
        int index = x % MOD;
        vector<int> &bucket = table[index];

        for (auto it = bucket.begin(); it != bucket.end(); it++) {
            if (*it == x) {
                bucket.erase(it);
                break;
            }
        }
    }

public:
    HashTable() : table(MOD) {}

    void start() {
        fin >> n;

        for (int i = 0; i < n; i++) {
            int op, x;
            fin >> op >> x;

            if (op == 1) {
                insert(x);
            } else if (op == 2) {
                remove(x);
            } else {
                fout << search(x) << '\n';
            }
        }
    }
};

int main() {
    HashTable hashTable;
    hashTable.start();
    return 0;
}