Cod sursa(job #1337600)

Utilizator cautionPopescu Teodor caution Data 9 februarie 2015 11:43:58
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include <fstream>
#include <iostream>
#include <list>
#define MOD 666013
using namespace std;
list<long> hash[MOD];
long n;
long h(long x)
{
    return x%MOD;
}
list<long>::iterator search(long x)
{
    long h_x=h(x);
    for(list<long>::iterator it=hash[h_x].begin(), end=hash[h_x].end(); it!=end; ++it)
        if((*it)==x) return it;
    return (list<long>::iterator)0;
}
list<long>::iterator add(long x)
{
    long h_x=h(x);
    if(search(x)==(list<long>::iterator)0)
    {
        hash[h_x].push_back(x);
        return hash[h_x].end();
    }
    return (list<long>::iterator)0;
}
short remove(long x)
{
    long h_x=h(x);
    list<long>::iterator pos=search(x);
    if(pos!=(list<long>::iterator)0)
    {
        hash[h_x].erase(pos);
        return 1;
    }
    return 0;
}
void print()
{
    for(long i=0; i<MOD; ++i)
        if(!hash[i].empty())
        {
            for(list<long>::iterator it=hash[i].begin(), end=hash[i].end(); it!=end; ++it)
                cout<<(*it)<<' ';
            cout<<'\n';
        }
}
int main()
{
    ifstream in("hashuri.in");
    ofstream out("hashuri.out");
    short op, x;
    in>>n;
    for(long i=0; i<n; ++i)
    {
        in>>op>>x;
        switch(op)
        {
            case 1: add(x);
            break;
            case 2: remove(x);
            break;
            case 3: if(search(x)!=(list<long>::iterator)0) out<<"1\n";
                    else out<<"0\n";
            break;
        }
       //cout<<'\n';
       // print();
    }
    in.close(); out.close();
    return 0;
}