Cod sursa(job #2253793)

Utilizator test_accNo Name test_acc Data 4 octombrie 2018 13:30:00
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <fstream>
#include <list>
 
using namespace std;
 
ifstream f("hashuri.in");
ofstream g("hashuri.out");
 
class Hash {
private:
    static const int HASHSIZE = 666013;
    list<int> hash[HASHSIZE];
 
    inline int hash_func(int key) {
        return key % HASHSIZE;
    }
    list<int>::iterator find_it(int bucket, int searched_key) {
        for (auto it = hash[bucket].begin(); it != hash[bucket].end(); ++it)
            if (searched_key == *it)
                return it;
        return hash[bucket].end();
    }
 
public:
    Hash();
    void ins(int key);
    void del(int key);
    int find(int key);
};
 
Hash::Hash() {
 
}
 
void Hash::ins(int key) {
    int bucket = hash_func(key);
    if (find_it(bucket, key) == hash[bucket].end())
        hash[bucket].push_back(key);
}
 
void Hash::del(int key) {
    int bucket = hash_func(key);
    auto it = find_it(bucket, key);
    if (it != hash[bucket].end())
        hash[bucket].erase(it);
}
 
int Hash::find(int key) {
    int bucket = hash_func(key);
    if (find_it(bucket, key) != hash[bucket].end())
        return 1;
    return 0;
}
 
int main() {
    int inps;
    f >> inps;
 
    int op, key;
    Hash *hash = new Hash();
    for (int i = 1; i <= inps; i++) {
        f >> op >> key;
        switch (op) {
        case 1:
            hash->ins(key);
            break;
        case 2:
            hash->del(key);
            break;
        case 3:
            g << hash->find(key) << "\n";
            break;
        }
    }
}