Cod sursa(job #1573535)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 19 ianuarie 2016 19:31:40
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
using namespace std;

const int PRIME = 1999993;
const int HASH_SIZE = PRIME;

vector<int> h[HASH_SIZE];

int hashFunction(int x)
{
    return x % PRIME;
}

bool hashFind(int x, vector<int> h[])
{
    bool found = false;
    int pos = hashFunction(x);
    for (int i = 0; i < h[pos].size() && !found; i++)
        if (h[pos][i] == x)
            found = true;
    return found;
}

void hashInsert(int x, vector<int> h[])
{
    if (!hashFind(x, h))
        h[hashFunction(x)].push_back(x);
}

void hashRemove(int x, vector<int> h[])
{
    int pos = hashFunction(x);
    for (int i = 0; i < h[pos].size(); i++)
        if (h[pos][i] == x)
        {
            h[pos][i] = h[pos].back();
            h[pos].pop_back();
        }
}

int main()
{
    int N, op, x, i;

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

    f >> N;

    for (i = 0; i < N; i++)
    {
        f >> op >> x;
        if (op == 1)
            hashInsert(x, h);
        else if (op == 2)
            hashRemove(x, h);
        else if (op == 3)
            g << hashFind(x, h) << '\n';
    }
    f.close();
    g.close();

    return 0;
}