Cod sursa(job #760301)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 20 iunie 2012 21:01:51
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.71 kb

#include <fstream>
#include <vector>
#include <list>

static const unsigned int HASH_KEY(666013);

inline unsigned int hash (unsigned int number)
{
    return number % HASH_KEY;
}

typedef std::list<unsigned int> chain;
typedef std::vector<chain> hash_table;

inline bool search_hash (const hash_table &table, const unsigned int number)
{
    chain::const_iterator it(table[hash(number)].begin()), end(table[hash(number)].end());
    while (it != end)
        if (*it == number)
            return true;
        else
            ++it;
    return false;
}

inline void hash_add (hash_table &table, const unsigned int number)
{
    if (!search_hash(table,number))
        table[hash(number)].push_front(number);
}

inline void hash_remove (hash_table &table, const unsigned int number)
{
    chain::iterator it(table[hash(number)].begin()), end(table[hash(number)].end());
    while (it != end)
        if (*it == number)
        {
            table[hash(number)].erase(it);
            break;
        }
        else
            ++it;
}

int main (void)
{
    unsigned int data_size;
    std::ifstream input("hashuri.in");
    input >> data_size;
    char operation;
    unsigned int number;
    hash_table table(HASH_KEY);
    std::ofstream output("hashuri.out");
    do
    {
        input >> operation >> number;
        if (operation == '1')
            hash_add(table,number);
        else if (operation == '2')
            hash_remove(table,number);
        else
        {
            if (search_hash(table,number))
                output.put('1');
            else
                output.put('0');
            output.put('\n');
        }
        --data_size;
    }
    while (data_size);
    input.close();
    output.close();
    return 0;
}