Cod sursa(job #3358876)

Utilizator adri22adria gram adri22 Data 21 iunie 2026 10:55:58
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.92 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

const int MOD = 666013;

struct Node {
    int val;
    int next;
};

int head[MOD];    
Node nodes[1000005];
int node_count = 0;

int get_hash(int x) {
    int cifre[11];
    int nr_cifre = 0;
    
    if(x == 0)
    {
        cifre[nr_cifre++] = 0;
    }
    else
    {
        while(x > 0)
        {
            cifre[nr_cifre++] = x % 10;
            x /= 10;
        }
    }
    long long hash_val = 0;
    for(int i = nr_cifre - 1; i >= 0; i--)
    {
        hash_val = (hash_val * 37 + cifre[i]) % MOD;
    }
    
    return hash_val;
}

void op1(int x)
{
    int h = get_hash(x);
    for(int i = head[h]; i > 0; i = nodes[i].next)
    {
        if(nodes[i].val == x)
            return;
    }
    node_count++;
    nodes[node_count].val = x;
    nodes[node_count].next = head[h];
    head[h] = node_count;
 
}

void op2(int x)
{
    int h = get_hash(x);
    int prev = 0;
    for(int i = head[h]; i > 0; i = nodes[i].next)
    {
        if(nodes[i].val == x)
        {
            if(prev == 0)
                head[h] = nodes[i].next;
            else
                nodes[prev].next = nodes[i].next;
            return;
        }
        prev = i;
    }
}

bool op3(int x)
{
    int h = get_hash(x);
    for(int i = head[h]; i > 0; i = nodes[i].next)
    {
        if(nodes[i].val == x)
            return true;
    }
    return false;
}
    
int main()
{
    int n; 
    fin >> n;
    for(int i = 0; i < n; i++)
    {
        int op, x;
        fin >> op >> x;
        if(op == 1)
            op1(x);
        if(op == 2)
            op2(x);
        if(op == 3)
        {
            if(op3(x))
                fout << "1" << '\n';
            else
                fout << "0" << '\n';
        }
    }
    
    return 0;
}