Cod sursa(job #921771)

Utilizator alinaelenaFMI Colceag Alina alinaelena Data 21 martie 2013 13:04:37
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.64 kb
#include <cstdio>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
template <class mytype>
class hash{
    mytype *h1;
    mytype *h2;
    int size;
    int nc;
    int const1,const2;
    int a1,b1;
    int a2,b2;
 
public:
    hash(mytype);
    ~hash();
    void setnull();
    int operator ^=(mytype);
    int poz1(mytype);
    int poz2(mytype);
    int operator -=(mytype);
    int operator ==(mytype);
    void rehash();
    void menu();
};
 
template <class mytype>
hash<mytype>::~hash()
{
    delete[] h1;
    delete[] h2;
}
 
template <class mytype>
void hash<mytype>::menu ()
 {
      int n,op,x;
     srand(time(NULL));
 
 
    int ok = 0;
    while (ok == 0)
    {
        ok = 1;
        rehash();
        FILE *in = fopen("hashuri.in","r");
        FILE *out = fopen("hashuri.out","w");
        fscanf(in,"%d",&n);
        setnull();
 
        while(n!=0)
        {
            fscanf(in,"%d%d",&op,&x);
            if(op==1)
            {
                if(((*this)^=x)==0)
                {
                    ok = 0;
                    break;
                }
            }
 
            if(op==2)
            {
                (*this)-=x;
            }
 
            if(op==3)
            {
                fprintf(out,"%d\n",*this == x);
            }
            n--;
        }
        fclose(in);
        fclose(out);
    }
 
 }
 
template <class mytype>
hash<mytype>::hash(mytype x)
{
    h1=new mytype[x];
    h2=new mytype[x];
        h1[3] = 4;
    nc=30;
     a1= 100000009;
     a2= 6666013;
     b1= 8976578;
     b2= 9999678;
     size=x;
 
 
}
 
template <class mytype>
void hash<mytype>::rehash()
{
    const1=a1+rand()%b1;
    const2=a2+rand()%b2;
}
 
template <class mytype>
void hash<mytype>::setnull()
{
    int i;
    for(i=0;i<size;++i)
        h1[i] = h2[i] = 0;
}
 
 template <class mytype>
 int hash<mytype>::operator ^= (mytype x)
{
    int i; 
	mytype aux;
    int care=1;
 
    if (*this == x)
    {
        return 1;
    }
 
    for(i=1;i<=nc;i++)
    {
        int hash_1 = poz1(x);
        int hash_2 = poz2(x);
        if(h1[hash_1]==0)
        {
            h1[hash_1]=x;
            return 1;
        }
        else if(h2[hash_2]==0)
        {
                h2[hash_2]=x;
                return 1;
        }
        else if (care==1)
        {
                aux=h1[hash_1];
                h1[hash_1]=x;
                x=aux;
        }
        else
        {
                aux=h2[hash_2];
                h2[hash_2]=x;
                x=aux;
        }
        care=-care;
    }
    return 0;
}
 
 
template <class mytype>
int hash<mytype>::poz1(mytype x)
{
    return (x%(long long)const1) % (long long)size;
}
 
template <class mytype>
int hash<mytype>::poz2(mytype x)
{
    return (x%(long long)const2) % (long long)size;
}

template <class mytype>
int hash<mytype>::operator == (mytype x)
{
    int hash_1 = poz1(x);
    int hash_2 = poz2(x);
    if(h1[hash_1]==x || h2[hash_2]==x)
        return 1;
        return 0;
}
 
 
 
 
template <class mytype>
int hash<mytype>::operator -=(mytype x)
{
    int hash_1 = poz1(x);
    int hash_2 = poz2(x);
 
    if(h1[hash_1]==x)
    {
        h1[hash_1]=0;
        return 1;
    }
    if(h2[hash_2]==x)
    {
        h2[hash_2]=0;
        return 1;
    }
    return 0;
}
 
 
 
 
int main()
{
//    freopen("hashuri.in","r",stdin);
//    freopen("hashuri.out","w",stdout);
    hash<int> *c;
    c = new hash <int> (1000000);
    c->menu();
    return 0;
 
}