Cod sursa(job #832307)

Utilizator AndrewTheGreatAndrei Alexandrescu AndrewTheGreat Data 10 decembrie 2012 12:26:16
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
///timing 11:53 - 30 minutes
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

class hash {
    int Key;
    vector <int> * H;
    vector<int>::iterator it;

    public:
        hash() { Key = 0; H = new vector<int>[1]; };
        hash(int);
        void insert(int);
        void erase(int);
        int check(int);
};

hash :: hash(int K){
    Key = K;
    H = new vector <int> [K];
}

void hash :: insert (int value) {
    int X = value % Key;
    H[X].push_back(value);
}

void hash :: erase (int value) {
    int X = value % Key;
    for(it = H[X].begin(); it != H[X].end(); it++)
        if(*it == value){
            H[X].erase(it);
            break;
        }
}

int hash :: check(int value) {
    int X = value % Key;
    for(it = H[X].begin(); it != H[X].end(); it++)
        if(*it == value) return 1;
    return 0;
}

int main()
{
    hash A(666013);

    int N, operation, value;
    in >> N;

    while(N--){
        in >> operation >> value;
        switch (operation) {
            case 1: A.insert(value); break;
            case 2: A.erase(value); break;
            case 3: out << A.check(value) << "\n"; break;
        }
    }

    return 0;
}