Cod sursa(job #2254479)

Utilizator AndreiJJIordan Andrei AndreiJJ Data 5 octombrie 2018 14:37:39
Problema Hashuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#include <vector>
using namespace std;
/// hash
const int p = 777013;
vector <int> h[p]; /// hash-ul
ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");
void Add (int x) /// adauga pe x in hash
{
    int r;
    r = x % p;
    h[r].push_back(x);
}
bool Search (int x) /// cauta pe x in hash
{
    int r;
    r = x % p;
    for (auto v : h[r])
        if (v == x)
            return true;
    return false;
}
void Delete (int x) /// sterge pe x din hash
{
    int r, L, i;
    r = x % p;
    L = h[r].size();
    for (i = 0; i < L; i++)
        if (x == h[r][i])
        {
            h[r][i] = h[r][L-1];
            h[r].pop_back();
            return;
        }
}
int main()
{
    int n, i, task, x;
    fin >> n;
    for (i = 1; i <= n; i++)
    {
        fin >> task >> x;
        if (task == 1 && !Search(x))
            Add(x);
        else if (task == 2)
            Delete(x);
        else
            fout << Search(x) << "\n";
    }
    fout.close();
    return 0;
}