Cod sursa(job #644250)

Utilizator ariel_roAriel Chelsau ariel_ro Data 5 decembrie 2011 20:46:28
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <vector>

#define M 666013

using namespace std;

vector<int> hash[M];

int hashFunc(int x)
{
    return x % M;
}

int find(int x)
{
    int poz = hashFunc(x);
    int length = hash[poz].size();
    for (int i = 0; i < length; i++)
    {
        if (hash[poz][i] == x)
            return 1;
    }
    return 0;
}

void insert(int x)
{
    int poz = hashFunc(x);
    int found = find(x);
    if (found == 0)
    {
        hash[poz].push_back(x);
    }
}

void del(int x)
{
    int poz = hashFunc(x);
    int found = find(x);

    if (found == 1)
    {
        hash[poz].pop_back();
    }
}

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

    int N, op, x;
    for (scanf("%d", &N); N; --N)
    {
        scanf("%d %d", &op, &x);
        if (op == 1) // inserare
        {
            insert(x);
        }
        else if (op == 2)   // stergere
        {
            del(x);
        }
        else if (op == 3)
        {
            printf("%d\n", find(x) != 0);
        }
    }
    return 0;
}