Cod sursa(job #1247796)

Utilizator szabibibiOrban Szabolcs szabibibi Data 23 octombrie 2014 21:39:47
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <unordered_map>

using namespace std;

#define PRIME_MAX 104729

std::vector<int> hash_table[PRIME_MAX];

int getHash(int val)
{
    return val % PRIME_MAX;
}

void InsertElement(int num)
{
    std::vector<int> &list = hash_table[getHash(num)];
    std::vector<int>::iterator it = std::find(list.begin(), list.end(), num);
    if (it == list.end())
    {
        list.push_back(num);
    }
}

void RemoveElement(int num)
{
    std::vector<int> &list = hash_table[getHash(num)];
    std::vector<int>::iterator it = std::find(list.begin(), list.end(), num);
    if (it != list.end())
    {
        list.erase(it);
    }
}

int FindElement(int num)
{
    std::vector<int> &list = hash_table[getHash(num)];
    std::vector<int>::iterator it = std::find(list.begin(), list.end(), num);
    if (it == list.end())
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int main()
{
    ifstream f("hashuri.in");
    ofstream g("hashuri.out");
    int n;

    f >> n;

    for (int i = 0; i < n; i++)
    {
        int com, arg;
        f >> com >> arg;

        if (com == 1)
            InsertElement(arg);
        else if (com == 2)
            RemoveElement(arg);
        else if (com == 3)
            g << FindElement(arg) << endl;
    }
    return 0;
}