Cod sursa(job #2744620)

Utilizator MihaelaDanilaDanila Mihaela MihaelaDanila Data 24 aprilie 2021 21:49:58
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

#define PRIM 7213

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

int n,val,operatie,viz[10000];

vector<int>::iterator it;
vector<vector<int>> h;

int hash_func(int cheie){
    return cheie%PRIM;
}

void push(int x){
    if(viz[x] == 0){
        return;
    }
    int poz = hash_func(x);
    h[poz].push_back(x);
}

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

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;
}

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