Cod sursa(job #2896893)

Utilizator Ilie_MityIlie Dumitru Ilie_Mity Data 1 mai 2022 13:54:10
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
//Ilie Dumitru
#include<cstdio>
typedef long long int ll;
const int NMAX=55;
const ll MOD=1000000007;

FILE* f=fopen("hashuri.in", "r"), *g=fopen("hashuri.out", "w");

const unsigned int P=666013;
unsigned int hash(const unsigned int x)
{
    return (41*x+5)%P;
}

struct node
{
    int x;
    node* n;
};

void push(node*& head, int x)
{
    node* n=new node;
    n->n=head;
    n->x=x;
    head=n;
}

void pop(node*& head, int x)
{
    if(head)
    {
        if(head->x==x)
        {
            node* n=head;
            head=head->n;
            delete n;
        }
        else
        {
            node* n=head;
            while(n->n && n->n->x!=x)
                n=n->n;
            if(n->n)
            {
                node* d=n->n;
                n->n=n->n->n;
                delete d;
            }
        }
    }
}

bool check(node* head, int x)
{
    while(head)
    {
        if(head->x==x)
            return 1;
        head=head->n;
    }
    return 0;
}

node* hashTable[P];

int main()
{
    int n, op, x, h;
    fscanf(f, "%d", &n);
    while(n--)
    {
        fscanf(f, "%d%d", &op, &x);
        h=hash(x);
        if(op==1)
        {
            if(!check(hashTable[h], x));
                push(hashTable[h], x);
        }
        else if(op==2)
            pop(hashTable[h], x);
        else
            fprintf(g, "%d\n", check(hashTable[h], x));
    }
    fclose(f);
    fclose(g);
    return 0;
}