Cod sursa(job #2224427)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 23 iulie 2018 23:01:17
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#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;
}