Cod sursa(job #1112353)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 19 februarie 2014 18:37:30
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

class Hash {
  public:
    Hash() {}

    void Insert(const int value) {
        if (!Find(value))
            table[GetKey(value)].push_back(value);
    }

    bool Find(const int value) const {
        int key = GetKey(value);
        for (vector<int>::const_iterator v = table[key].begin(); v != table[key].end(); ++v)
            if (*v == value)
                return true;
        return false;
    }

    void Erase(const int value) {
        int key = GetKey(value);
        for (vector<int>::iterator v = table[key].begin(); v != table[key].end(); ++v) {
            if (*v == value) {
                table[key].erase(v);
                return;
            }
        }
    }

  private:
    static const int U = 666013;

    vector<int> table[U];

    static int GetKey(const int value) {
        return value % U;
    }
};

int main() {
    ifstream cin("hashuri.in");
    ofstream cout("hashuri.out");
    Hash hash;
    int q;
    cin >> q;
    for (; q > 0; --q) {
        int type, value;
        cin >> type >> value;
        if (type == 1)
            hash.Insert(value);
        else if (type == 2)
            hash.Erase(value);
        else if (type == 3)
            cout << hash.Find(value) << "\n";
    }
    cin.close();
    cout.close();
    return 0;
}