Cod sursa(job #3132053)

Utilizator AlexTrandafir09Trandafir Alexandru Ionut AlexTrandafir09 Data 21 mai 2023 22:58:31
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.17 kb
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
const int mod = 500009;
struct nod
{
    int valoare = 0;
    nod* vecin = nullptr;
    nod()
    {
        valoare = 0;
        vecin = nullptr;
    }
    nod(long long valoare_)
    {
        valoare = valoare_;
        vecin = nullptr;
    }
}
;
nod* vec[mod + 5];
unsigned int hashuire(unsigned int x) {
    x = (((x >> 16) ^ x) * 0x45d9f3b) % mod;
    x = (((x >> 16) ^ x) * 0x45d9f3b) % mod;
    x = ((x >> 16) ^ x) % mod;
    return x;
}
void adaugare(long long numar, long long index)
{

    bool gasit = false;
    if(vec[index] == nullptr)
    {
        nod* nod_nou = new nod();
        vec[index] = nod_nou;
    }
    nod* y = vec[index];

    while(y ->vecin != nullptr)
    {y = y ->vecin;
        if(y ->valoare == numar)
            gasit = true ;
    }
    if(gasit == false)
    {   nod* nod_nou = new nod(numar);
        nod_nou ->vecin = nullptr;
        y ->vecin = nod_nou;
    }

}
void sterge(long long numar, long long index)
{
    if(vec[index] == nullptr) return ;
    nod* curent = vec[index];
    nod* ultimul = nullptr;
    bool gasit = false;
    while(curent ->vecin != nullptr)
    {
        ultimul = curent;
        curent = curent ->vecin;

        if(curent ->valoare == numar)
        {
            gasit = true;
            break;
        }
    }
    if(gasit == true)
    {
        ultimul ->vecin = curent ->vecin;
        delete curent;
    }
}
bool gasire(long long numar, long long index)
{
    nod* curent = vec[index];
    if(curent == nullptr) return false;
    while(curent ->vecin != nullptr)
    {
        curent = curent ->vecin;
        if(curent ->valoare == numar)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    int n;
    f >> n;
    while(n--)
    {
        int alegere;
        long long numar;
        f >> alegere >> numar;
        long long index = hashuire(numar);
        if(alegere == 1)
            adaugare(numar, index);
        if(alegere == 2)
            sterge(numar, index);
        if(alegere == 3)
        {
            g << gasire(numar, index) << '\n';
        }
    }
    return 0;
}