Cod sursa(job #1204561)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 3 iulie 2014 12:28:46
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

template <typename Type, int MOD>
class HashTable
{
private:

    vector < vector<Type> > HT;

public:

    HashTable()
    {
        HT = vector < vector<Type> >( MOD );
    }

    int func( const Type key )
    {
        return key % MOD;
    }

    bool find( const Type key )
    {
        int value = func( key );

        for ( auto it = HT[value].begin(); it != HT[value].end(); ++it )
            if ( *it == key )
                return true;

        return false;
    }

    void insert( const Type key )
    {
        int value = func( key );

        if ( find( key ) == false )
            HT[value].push_back( key );
    }

    void erase( const Type key )
    {
        int value = func( key );

        for ( auto it = HT[value].begin(); it != HT[value].end(); ++it )
            if ( *it == key )
            {
                HT[value].erase( it );
                break;
            }
    }
};

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

    HashTable <int, 666013> HT;

    in.sync_with_stdio( false );

    int N, type, key;

    in >> N;

    while ( N-- )
    {
        in >> type >> key;

        if ( type == 1 )
        {
            HT.insert( key );
        }

        if ( type == 2 )
        {
           HT.erase( key );
        }

        if ( type == 3 )
        {
            out << HT.find( key ) << "\n";
        }
    }

    return 0;
}