Cod sursa(job #1000722)

Utilizator alexx.cosmaCosma Cristian Alexandru alexx.cosma Data 23 septembrie 2013 17:29:30
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
const int N = 666013;
vector<int> h[N] ;

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<h[hashKey].size(); i++)
        if(h[hashKey].at(i) == nr)
        {
            inHashPosition=i;
            break;
        }
    return inHashPosition;
}

void insertInHash(int nr)
{
    h[getHash(nr)].push_back(nr);
}

void deleteFromHash(int nr, int position)
{
    h[getHash(nr)].erase(h[getHash(nr)].begin() + position);
}

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;
    }
    }
}