Cod sursa(job #2624333)

Utilizator CharmichlesAndrei Brihac Charmichles Data 4 iunie 2020 18:35:49
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.26 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

const string IN_FILE = "hashuri.in";
const string OUT_FILE = "hashuri.out";

const int MAX_BUCKETS = 100000;

class HashTable {
    public:
        vector<int>::iterator getValue(int val) {
            vector<int>* bucket = &table[myHash(val)];
            for (vector<int>::iterator it =  bucket->begin(); it != bucket->end(); it++) {
                if (*it == val) {
                    return it;
                }
            }
            return bucket->end();
        }
        void addValue(int val) {
            vector<int>* bucket = &table[myHash(val)];
            bool found = false;
            for (vector<int>::iterator it = bucket->begin(); it != bucket->end() && !found; it++) {
                if (*it == val) {
                    found = true;
                }
            }
            if (!found) {
                bucket->push_back(val);
            }
        }
        void eraseValue(int val) {
            vector<int>* bucket = &table[myHash(val)];
            for (vector<int>::iterator it =  bucket->begin(); it != bucket->end(); it++) {
                if (*it == val) {
                    bucket->erase(it);
                    break;
                }
            }
        }
        bool exists(int val) {
            vector<int>* bucket = &table[myHash(val)];
            bool found = false;
            for (vector<int>::iterator it = bucket->begin(); it != bucket->end() && !found; it++) {
                if (*it == val) {
                    found = true;
                }
            }
            return found;
        }
    private:
        vector<int> table[MAX_BUCKETS];
        int myHash(int x) {return x % MAX_BUCKETS;}
};


int main() {
    ifstream fin(IN_FILE);
    ofstream fout(OUT_FILE);
    HashTable my_hash;
    int n, tip, x, i;
    fin >> n;
    for (i = 0; i < n; i++) {
        fin >> tip >> x;
        if (tip == 1) {
            my_hash.addValue(x);
        }
        if (tip == 2) {
            my_hash.eraseValue(x);
        }
        if (tip == 3) {
            fout << my_hash.exists(x) << '\n';
        }
    }
    fin.close();
    fout.close();
    return 0;
}