Cod sursa(job #2714345)

Utilizator vnedelcuVictor Andrei Nedelcu vnedelcu Data 1 martie 2021 18:22:12
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#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;
}