Cod sursa(job #1197012)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 10 iunie 2014 11:58:53
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.32 kb
/* vector STL */
using namespace std;
#include <fstream>
#include <vector>
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

const int Nmax = 1000000;
const int P = 666013;

vector<int> L[P];

void push(int) ;
void sterge(int) ;
bool search1(int) ;

int main()
{
    int i, a, tip, n;
    fin>>n;
    for(i=0; i<n; ++i)
    {
        fin>>tip>>a;
        if(tip==1) push(a);
        if(tip==2) sterge(a);
        if(tip==3) fout<<search1(a)<<'\n';
    }
    return 0;
}


void push(int x)
{
    int hash = x%P;
    for(vector<int>::iterator it = L[hash].begin(); it!=L[hash].end(); ++it)
        if(*it == x) return;
    L[hash].push_back(x);
}


void sterge(int x)
{
    int hash = x%P;
    for(vector<int>::iterator it = L[hash].begin(); it!=L[hash].end(); ++it)
        if(*it == x)
        {
            L[hash].erase(it);
            return;
        }
}


bool search1(int x)
{
    int hash = x%P;
    for(vector<int>::iterator it = L[hash].begin(); it!=L[hash].end(); ++it)
        if(*it == x) return 1;
    return 0;
}



/* liste simplu inlantuite */
/*using namespace std;
#include <fstream>
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

const int Nmax = 1000000;
const int P = 666013;

struct _nod {int x; nod* urm;} ;
_nod* prim[P];

void push(int) ;
void sterge(int) ;
bool search1(int) ;

int main()
{
    int i, a, tip, n;
    for(i=0; i<P; ++i) prim[i]->urm = NULL; prim[i]->x = 0;
    fin>>n;
    for(i=0; i<n; ++i)
    {
        fin>>tip>>a;
        if(tip==1) push(a);
        if(tip==2) sterge(a);
        if(tip==3) fout<<search1(a)<<'\n';
    }
    return 0;
}


void push(int x)
{
    int hash = x%P;
    if(prim[hash]->x == 0) {prim[hash]->x = x; return;}
    for(_nod *it = prim[hash]; it->urm != NULL; it = it->urm)
        if(it->x == x) return;
    if(it->x == x) return;

    _nod *nou = new _nod;
    it->urm = nou; nou->urm = NULL;
    nou->x = x;
}


void sterge(int x)
{
    int hash = x%P;
    for(_nod *it = prim[hash]; it != NULL; it = it->urm)
        if(*it == x)
        {
            L[hash].erase(it);
            return;
        }
}


bool search1(int x)
{
    int hash = x%P;
    for(_nod *it = prim[hash]; it != NULL; it = it->urm)
        if(it->x == x) return 1;
    return 0;
}
*/