Cod sursa(job #904943)

Utilizator UnforgivenMihai Catalin Botezatu Unforgiven Data 5 martie 2013 08:34:34
Problema Hashuri Scor 30
Compilator c Status done
Runda Arhiva educationala Marime 3.17 kb
//Cuckoo Hashing


#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int func_hash1(int value, int modulo)
{
    return value % modulo;
}

int func_hash2(int value , int modulo)
{
    return (int)(value/modulo) % modulo;
}

int find(int value , int modulo , int *hash1 , int *hash2)
{
    int h1 = func_hash1(value,modulo);
    int h2 = func_hash2(value,modulo);
    if (hash1[h1] == value || hash2[h2] == value)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void erase(int value , int modulo , int *hash1 , int *hash2)
{
     int h1 = func_hash1(value,modulo);
     int h2 = func_hash2(value,modulo);
     if (hash1[h1] == value)
     {
        hash1[h1] = 0;
     }
     else if (hash2[h2] == value)
     {
        hash2[h2] = 0;
     }
}

int insert(int value , int modulo , int *hash1 , int *hash2)
{
    int init = value;
    char which = 1;
    int iteratii = 0;
    while (value != 0)
    {
        iteratii++;
        int h1 = func_hash1(value,modulo);
        int h2 = func_hash2(value,modulo);
        if (hash1[h1] == 0)
        {
            hash1[h1] = value;
            value = 0;
        }
        else if(hash2[h2] == 0)
        {
            hash2[h2] = value;
            value = 0;
        }
        else
        {
            if (which == 1)
            {
                int aux = value;
                value = hash1[h1];
                hash1[h1] = aux;
            }
            else
            {
                int aux = value;
                value = hash2[h2];
                hash2[h2] = aux;
            }
            which = - which;
        }
        if (iteratii > 19)
        {
            return 0;
        }
    }

    return 1;
}

void fill(int *vect , int size)
{
    int i;
    for (i =0;i<size;i++)
    {
        vect[i] = 0;
    }
}


int rehash()
{
    int end = 0;
    int n;
    int size = 666013;
    int modulo = rand() % size;
    int *hash1 = (int*)malloc(size * sizeof(int));
    int *hash2 = (int*)malloc(size * sizeof(int));
    int *print = (int*)malloc(size * sizeof(int));
    int h =0;
    do
    {
        FILE *input = fopen("hashuri.in","r");
        modulo = rand() % (size-1) + 1;
        fill(hash1,size);
        fill(hash2,size);
        h = 0;
        int i;
        fscanf(input,"%d",&n);
        for (i=0;i<n;i++)
        {
            int x , op;
            fscanf(input,"%d%d",&op,&x);
            if (op == 1)
            {
                if ( insert(x,modulo,hash1,hash2) == 0)
                {
                    end = 1;
                    break;
                }
            }
            if (op == 2)
            {
                erase(x,modulo,hash1,hash2);
            }
            if (op == 3)
            {
                print[h]=find(x,modulo,hash1,hash2);
                h++;
            }
        }


        fclose(input);
    }while(end == 1);

    FILE *output = fopen("hashuri.out","w");
    int i = 0;
    for (i =0;i<h;i++)
    {
        fprintf(output,"%d\n",print[i]);
    }


}

int main()
{
    srand(time(0));
    rehash();
    return 0;

}