Cod sursa(job #922132)

Utilizator costel93FMI - Dumea Eduard Constantin costel93 Data 21 martie 2013 22:27:15
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 4.68 kb
//Dumea Eduard Constantin, grupa 134

#include<iostream>
#include<fstream.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>

using namespace std;

class Functie
{
private:
    int a , b;
    int mod;
    int size;
public:
    Functie(int &);
    void generare_parametri();
    int functie(int&);
	int functie(unsigned int&);
    int functie(float&);
    int functie(double&);
    int functie(char&);
    int functie(string&);
};
 
Functie::Functie(int &n)
{	
    size = n;
    generare_parametri();
}
 
void Functie::generare_parametri()
{
	int pr[]={999983,999979,899981,900007,800011};
    a = rand() % 1000000+10;
    b = rand() % 1030000+10;
	mod = pr[rand () % 5];
}
 
int Functie::functie(char &val)
{
	int poz;
	poz = ((a + b*val ) % mod) % size;
    return poz;
}
 
int Functie::functie(int &val)
{
	int poz;
	poz=((a + b*val ) % mod) % size;
    return poz;
}

int Functie::functie(unsigned int &val)
{
	int poz;
	poz=((a + b*val ) % mod) % size;
    return poz;
}
 
int Functie::functie(float &val)
{
	int poz;
    while ((val - (int)val) != 0)
    {
        val *= 10;
        if (val > size)
        {
            val -= size;
        }
    }
	poz = (((long)a + (long)b*(long)val ) % (long)mod) % (long)size;
	return poz;
}
 
int Functie::functie(double &val)
{
	int poz;
    while ((val - (int)val) != 0)
    {
        val *= 10;
        if (val > size)
        {
            val -= size;
        }
    }
	poz = (((long)a + (long)b*(long)val ) % (long)mod) % (long)size;
	return poz;
}
 
int Functie::functie(string &val)
{
    int aux = 0;
    for (int i =0;i<val.size();i++)
        aux = (aux+a + b*val[i])%size; 
    return aux;
}

template <class type>
class Hash
{
    int mod, size;
    type *hash1, *hash2;
    bool *viz1, *viz2;
    Functie *func1, *func2;
 
    public:
    Hash<type>(int);
    ~Hash<type>();
    void initializare_viz();
	void rezolvare();
    bool operator += (type&);
    void operator -= (type&);
    bool operator == (type&);
};


template <class type>
Hash<type>::Hash(int n)
{
	size = n;
    hash1 = new type[size];
    hash2 = new type[size];
    func1 = new Functie(size);
    func2 = new Functie(size);
	viz1 = new bool[size];
    viz2 = new bool[size];
 
    fill(viz1,viz1+size,0);
    fill(viz2,viz2+size,0);
}
 

template <class type>
Hash<type>::~Hash()
{
    delete[] hash1;
    delete[] hash2;
    delete[] viz1;
    delete[] viz2;
}

template <class type>
void Hash<type>::initializare_viz()
{
    fill(viz1,viz1+size,0);
    fill(viz2,viz2+size,0);
}



template <class type>
bool Hash<type>::operator += (type &val)
{
	int nr=0, poz1, poz2;
	type aux;
	
	poz1 = func1->functie(val);
	poz2 = func2->functie(val);
	
	if( (hash1[poz1]==val && viz1[poz1] ==1 ) || ( hash2[poz2]==val && viz2[poz2]==1 ) )
		return 1;
	
	if(viz1[poz1]==0)
	{
		hash1[poz1]=val;
		viz1[poz1]=1;
		return 1;
	}
	else
		if(viz2[poz2]==0)
		{
			hash2[poz2]=val;
			viz2[poz2]=1;
			return 1;
		}
		else
		{
			aux = val;
			val = hash1[poz1];
			hash1[poz1] = aux;
		}
	
	while(nr<30)
	{
		nr++;
		
		poz2 = func2->functie(val);
		
		if(viz2[poz2]==0)
		{
			hash2[poz2]=val;
			viz2[poz2]=1;
			return 1;
		}
		else
		{
			poz1 = func1->functie(hash2[poz2]);
			aux=hash2[poz2];
			hash2[poz2]=val;
			val=hash1[poz1];
			hash1[poz1]=aux;
		}
	}
	
	return 0;
}		
	

template <class type>	
void Hash<type>::operator -= (type &val)
{
	int poz1, poz2;
	
	poz1 = func1->functie(val);
	poz2 = func2->functie(val);
	
	if( ( hash1[poz1] == val ) && viz1[poz1] == 1 )
		viz1[poz1]=0;
		
	else
		if( hash2[poz2] == val && viz2[poz2] == 1 )
			viz2[poz2] = 0;
}


template <class type>
bool Hash<type>::operator == (type &val)
{
	int poz1, poz2;
	
	poz1= func1->functie(val);
	poz2= func2->functie(val);
	
	if( ( hash1[poz1] == val && viz1[poz1] == 1) || ( hash2[poz2] == val && viz2[poz2] == 1) )
		return 1;
	else
		return 0;
}
	
template <class type>
void Hash<type>::rezolvare()
{
    type val;
	
	int n, op, ok=0, i;
	
	srand( time( NULL ) );
	
	
	while(ok==0)
	{
		initializare_viz();
		
		func1->generare_parametri();
		func2->generare_parametri();
		
		ifstream f("hashuri.in");
		ofstream g("hashuri.out");
		
		f>>n;
		
		ok=1;
	
		for(i=1; i <= n && ok; i++)
		{
			f>>op>>val;
			
			//cout<<i<<" "<<op<<" "<<val<<"\n";
		
			if(op==1)
				ok = *this += val;
			else
				if(op==2)
					*this -= val;
				else
					g<<(*this==val)<<"\n"; 
		}
		
	}
}
int main()
{
	Hash<unsigned int> h(1000000);

    h.rezolvare();
	
    return 0;
}