Cod sursa(job #3359829)

Utilizator Sabin1133Padurariu Sabin Sabin1133 Data 4 iulie 2026 22:16:40
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.81 kb
#include <iostream>
#include <algorithm>

#define MOD 1000003

#define BUCKET_SIZE 2000
#define NMAX MOD

int hash_table_sizes[NMAX] = {0};
int hash_table[NMAX][BUCKET_SIZE] = {0};

int hash(int x)
{
    return x % MOD;
}

void hash_table_insert(int (*hash_table)[BUCKET_SIZE], int *hash_table_sizes, int x)
{
    int &size = hash_table_sizes[hash(x)];
    int (&bucket)[BUCKET_SIZE] = hash_table[hash(x)];

    for (int i = 0; i < size; ++i)
        if (bucket[i] == x)  
            return;

    bucket[size++] = x;
}

void hash_table_delete(int (*hash_table)[BUCKET_SIZE], int *hash_table_sizes, int x)
{
    int &size = hash_table_sizes[hash(x)];
    int (&bucket)[BUCKET_SIZE] = hash_table[hash(x)];

    for (int i = 0; i < size; ++i)
        if (bucket[i] == x) {
            --size;

            for (int j = i; j < size; ++j)
                bucket[j] = bucket[j + 1];

            return;
        }
}

bool hash_table_search(int (*hash_table)[BUCKET_SIZE], int *hash_table_sizes, int x)
{
    int &size = hash_table_sizes[hash(x)];
    int (&bucket)[BUCKET_SIZE] = hash_table[hash(x)];

    for (int i = 0; i < size; ++i)
        if (bucket[i] == x)
            return true;

    return false;
}

int main()
{
    int n;

    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    std::cin >> n;

    for (int op, x, i = 0; i < n; ++i)
    {
        std::cin >> op >> x;

        switch (op) {
        case 1:
            hash_table_insert(hash_table, hash_table_sizes, x);    

            break;
        case 2:
            hash_table_delete(hash_table, hash_table_sizes, x);

            break;
        case 3:
            if (hash_table_search(hash_table, hash_table_sizes, x))
                std::cout << 1;
            else
                std::cout << 0;    

            break;
        }
    }

    return 0;
}