Cod sursa(job #2745302)

Utilizator gabrielanideleaNidelea Gabriela-Andreea gabrielanidelea Data 26 aprilie 2021 12:50:11
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
vector<int> hashh[1000];
int n, op, x;
bool semafor(int x){
    int poz = x%271;
    for(int i=0;i<hashh[poz].size();i++){
        if(hashh[poz][i] == x) return true; // daca x se gaseste in hash, returnam true
    }
    return false; // daca nu, false
}
void push(int x){
    if(semafor(x)!=0){
        hashh[x%271].push_back(x); // adaugam x-ul in hash
    }
}
void pop(int x){
    int poz = x%271;
    for(int i=0;i<hashh[poz].size();i++){
        if(hashh[poz][i]==x){
            hashh[poz].erase(hashh[poz].begin()+i); // stergem x-ul din  hash
            return;
        }
    }
}

int main(){

    f>>n;
    for(int i=0;i<n;i++){
        f>>op>>x; // citesc fiecare pereche (operatie, numar)
        if(op == 1)
            push(x); // 1 => adaug cu push
        else{
            if(op == 2)
                pop(x); // 2 => sterg cu pop
            else
                g<<semafor(x)<<endl; // pe varianta 3 afisez

        }
        }
    return 0;
}