Cod sursa(job #643075)

Utilizator ariel_roAriel Chelsau ariel_ro Data 2 decembrie 2011 21:06:45
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <iostream>
#include <fstream>
#include <vector>

#define M 600000

using namespace std;

vector<int> hash[M];

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

void insert(int x)
{
    int poz = hashFunc(x);
    if (hash[poz].size() > 0)
    {
        for (unsigned int i = 0; i < hash[poz].size(); i++)
        if (hash[poz][i] == x)
            return;
    }
    hash[poz].push_back(x);
}

void del(int x)
{
    int poz = hashFunc(x);
    if (hash[poz].size() > 0)
    {
        for (unsigned int i = 0; i < hash[poz].size(); i++)
        {
            if (hash[poz][i] == x)
            {
                hash[poz].erase(hash[poz].begin() + i);
            }
        }
    }
}

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

int main()
{
    FILE *fin = fopen("hashuri.in", "r");
    FILE *fout = fopen("hashuri.out", "w");

    int N, op, x;
    for (fscanf(fin, "%d", &N); N; --N)
    {
        fscanf(fin, "%d %d", &op, &x);
        if (op == 1) // inserare
        {
            insert(x);
            continue;
        }
        if (op == 2) // stergere
        {
            del(x);
            continue;
        }
        fprintf(fout, "%d\n", find(x));
    }
    return 0;
}