Cod sursa(job #907311)

Utilizator mariacMaria Constantin mariac Data 7 martie 2013 20:18:07
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 3.18 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;

int* aloca(int *h, int MOD)
{
    int i;
    free(h);
    h = (int *) malloc (MOD * sizeof(int));
    for (i = 0; i < MOD; i++)
        h[i] = 0;
    return h;
}

void SetHash (int &a, int &b, int &c, int &MOD)
{
    srand (time(NULL));

    a = rand () % 1000;
    b = rand () % 1000;
    c = rand () % 1000;
    MOD = rand () % 1000000;



}

int insereaza (int x, int a, int b, int c, int *h1, int *h2, int MOD)
{
    int ok = 0, st = 1, aux;

    while( ok > -50 )
    {
        if( st == 1 )
        {
            if(!h1 [(a * (x %MOD)) % MOD] || h1 [(a * (x % MOD)) % MOD] == x)
            {
                h1 [(a * (x % MOD)) % MOD] = x;
                return 1;

            }
            else
            {
                st = -1;
                if( ok < 0)
                {
                    aux = x;
                    x =  h1 [(a * (x % MOD)) % MOD];
                    h1 [(a * (x % MOD)) % MOD] = aux;
                }
            }
        }
        if( st == -1)
        {
            if(!h2 [((b * (x % MOD)) % MOD + c * c) % MOD] || h2 [((b * (x % MOD)) % MOD + c * c) % MOD] )
            {

                h2 [((b * (x % MOD)) % MOD + c * c) % MOD] = x;

                return 1;
            }

            else
            {
                st = 1;
                if( ok < 0)
                {
                    aux = h2 [((b * (x % MOD)) % MOD + c * c) % MOD];
                    h2 [((b * (x % MOD)) % MOD + c * c) % MOD] = x;
                    x = aux;
                }

            }

        }
        ok--;



    }

    return 0;


}
void del(int x, int a, int b, int c, int *h1, int *h2, int MOD)
{
    if(h1 [(a * (x % MOD)) % MOD] == x) h1 [(a * (x % MOD)) % MOD] = 0;

        else if(h2 [((b * (x % MOD)) % MOD + c * c) % MOD] == x ) h2 [((b * (x % MOD)) % MOD + c * c) % MOD] = 0;
}

int cauta(int x, int a, int b, int c, int *h1, int *h2, int MOD)
{
     if(h1 [(a * (x % MOD)) % MOD] == x) return 1;

     if(h2 [((b * (x % MOD)) % MOD + c * c) % MOD] == x ) return 1;

    return 0;
}

void citeste (int a, int b, int c, int *h1, int *h2, int MOD)
{
    FILE *f = fopen ("hashuri.in", "r");
    FILE *fi = fopen ( "hashuri.out", "w");
    int n, x, caz, i;

    fscanf (f,"%d", &n);

    for (i = 1; i <= n; i++)
    {
        fscanf (f, "%d %d", &caz, &x);

        switch (caz)
        {
            case 1: while(!insereaza( x, a, b, c, h1, h2, MOD))
                    {
                        SetHash (a, b, c, MOD);
                        aloca(h1, MOD);
                        aloca(h2, MOD);
                        citeste (a, b, c, h1, h2, MOD);

                    }
                    break;
           case 2: del(x, a, b, c, h1, h2, MOD); break;
           case 3: fprintf( fi, "%d\n", cauta(x, a, b, c, h1, h2, MOD)); break;

        }
    }
}




int main()
{
    int a, b, c, MOD;
    int *h1, *h2;
    SetHash (a, b, c, MOD);
    h1 = aloca(h1, MOD);
    h2 = aloca(h2, MOD);
    citeste (a, b, c, h1, h2, MOD);

    return 0;

}