Cod sursa(job #734263)

Utilizator yamahaFMI Maria Stoica yamaha Data 13 aprilie 2012 21:33:43
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 3.93 kb
#include <cstdio>
#include <cstdlib>
using namespace std;

#define max 1000003
#define prim 670013

template <class T> // asa arata un nod din lista
class node
{
    public:
           T key;
           node * next;
};

template <class T> // tabelul hash - stl implementat de mana
class table
{
    node<T> *v;
    int table_size;
    
    public:
           table();
           int size();
           bool empty();
           node<T> *first();
           node<T> *last();
           bool search(T);
           void insert(T);
           void erase(T);        
};

template <class T>
table<T> :: table()
{
    v = NULL; 
    table_size = 0;
}

template <class T>
int table<T> :: size()
{
    return table_size;
}

template <class T>
bool table<T> :: empty()
{
    if(table_size) return false;
    return true;
}

template <class T>
node<T> *table<T> :: first()
{
    return v;
}

template <class T>
node<T> *table<T> :: last()
{
    node<T> *p = v;
    while(p->next) p = p->next;
    return p;
}

template <class T>
bool table<T> :: search(T x)
{
    node<T> *p;
    if(empty()) return false;
    p = first();
    while(p)
    {
         if(p->key == x) return true;
         p = p->next;
    }
    return false;
}

template <class T>
void table<T> :: insert(T x)
{
    node<T> *p, *q;
    if(search(x)) return;
    
    q = new node<T>;
    q->key = x;
    q->next = NULL;
    
    if(empty()) v = q;
    else { p = last(); p->next = q; }
    
    table_size++;
}

template <class T>
void table<T> :: erase(T x)
{
    node<T> *p, *q;
    if(!search(x)) return;
    
    // daca exista in tabel
    if(table_size == 1) v = NULL;
    else
    {
         // daca e pe prima pozitie
         q = first();
         if(q->key == x) q = q->next;
         else // daca nu
         {
              p = q->next;
              while(p)
              {
                   if(p->key == x)
                   {
                        q->next = p->next;
                        delete p;
                        break;
                   }
                   q = p;
                   p = p->next;
              }
         }
    }
    
    table_size--;
}


template <class T>
class hash
{
    table<T> *H;
    int a, b;
    
    public:
           hash();
           void random();
           unsigned h(T);
           bool search(T);
           bool insert(T);
           bool erase(T);
};

template <class T>
hash<T> :: hash()
{
    H = new table<T> [max];
}

template <class T>
void hash<T> :: random()
{
     a = rand()%13; 
     b = rand()%13 + 1;
}

template <class T>
unsigned hash<T> :: h(T x)
{
     unsigned index = a*x+b;
     index %= prim;
     index %= max; 
     return index;
     //unsigned index = (unsigned)(a*x+b)%max; 
     //return index; //= (unsigned) index % prim;
     
}

template <class T>
bool hash<T> :: search(T x)
{
    unsigned index = h(x);
    if(H[index].search(x)) return 1;
    return 0;
}

template <class T>
bool hash<T> :: insert(T x)
{
    unsigned index = h(x);
    H[index].insert(x);
    return true;
}

template <class T>
bool hash<T> :: erase(T x)
{
    unsigned index = h(x);
    H[index].erase(x);
    return true;
}


int main ()
{
    //ifstream f("grader_test5.in");
    //ofstream g("hashuri.out");
    
    freopen ("hashuri.in", "r", stdin);
    freopen ("hashuri.out", "w", stdout);
    
    hash <int> hsh;
    //hsh.random();
    
    int N, i, t, x;
    //f>>N;
    scanf ("%d", &N);
    for(i=1;i<=N;++i)
    {
         //f>>t>>x;
         scanf ("%d%d", &t, &x); 
         if(t == 1)
              hsh.insert(x);
         else if(t == 2)
              hsh.erase(x);
         else if(t == 3)
         { 
              //g<<hsh.search(x)<<endl;
              printf("%d\n", hsh.search(x));
         }
    }
    //f.close(); g.close();
    return 0;
}