Cod sursa(job #2744650)

Utilizator MihaelaDanilaDanila Mihaela MihaelaDanila Data 24 aprilie 2021 22:17:36
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

vector<int>::iterator it;
vector<int> h[100];
int n, operatie, x;

int hash_func(int x){
    return x%31;
}

void pop(int x){
    int poz = hash_func(x);
    for(it=h[poz].begin();it!=h[poz].end();it++){
        if(*it == x){
            h[poz].erase(it);
        }
    }
}

int index(int x){
    int poz = hash_func(x);
    for(it=h[poz].begin();it!=h[poz].end();it++){
        if(*it == x){
            return 1;
        }
    }
    return 0;
}

void push(int x){
    int poz = x%31;
    if(index(x)){
        h[poz].push_back(x);
    }
}

int main(){
    f>>n;

    for(int i=0;i<n;i++){
        f>>operatie>>x;
        if(operatie == 1) push(x);
        if(operatie == 2) pop(x);
        if(operatie == 3) g<<index(x)<<'\n';
    }
    return 0;
}