Cod sursa(job #1000712)

Utilizator alexx.cosmaCosma Cristian Alexandru alexx.cosma Data 23 septembrie 2013 17:18:57
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <iostream>
#include <fstream>

using namespace std;
const int N = 666013;
int hash[N][20];

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

void doOp(int op, int param);

int main()
{

    int nrOp = 0;

    fin >> nrOp;

    for(int i=0; i<nrOp; i++)
    {
        int op,param;
        fin >> op;
        fin >> param;
        doOp(op,param);
    }

    return 0;
}

int getHash(int nr)
{
    return (nr%N);
}

int inHashTable(int nr)
{
    int inHashPosition = -1;
    int hashKey = getHash(nr);
    for(int i=0; i<20; i++)
        if(hash[hashKey][i] == nr)
        {
            inHashPosition=i;
            break;
        }
    return inHashPosition;
}

void insertInHash(int nr)
{
    int hashKey = getHash(nr);
    for(int i=0; i<20; i++)
    {
        if(hash[hashKey][i] == 0)
        {
            hash[hashKey][i] = nr;
            break;
        }
    }
}

void deleteFromHash(int nr, int position)
{
    hash[getHash(nr)][position]=0;
}

void doOp(int op, int param)
{

    switch(op)
    {

    case 1:
    {
        if(inHashTable(param) == -1)
            insertInHash(param);
        break;
    }
    case 2:
    {
        int inHashPosition = inHashTable(param);
        if(inHashPosition != -1)
        {
            deleteFromHash(param, inHashPosition);
        }
        break;
    }
    case 3:
    {
        if(inHashTable(param) != -1)
        {
            fout << '1' << '\n';
        }
        else
        {
            fout << '0' << '\n';
        }
        break;
    }
    }
}