Cod sursa(job #1337603)

Utilizator cautionPopescu Teodor caution Data 9 februarie 2015 11:45:45
Problema Hashuri Scor 30
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> mhash[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=mhash[h_x].begin(), end=mhash[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)
    {
        mhash[h_x].push_back(x);
        return mhash[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)
    {
        mhash[h_x].erase(pos);
        return 1;
    }
    return 0;
}
void print()
{
    for(long i=0; i<MOD; ++i)
        if(!mhash[i].empty())
        {
            for(list<long>::iterator it=mhash[i].begin(), end=mhash[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;
}