Cod sursa(job #2747207)

Utilizator carmenacatrineiCarmen-Lorena Acatrinei carmenacatrinei Data 28 aprilie 2021 22:07:42
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.02 kb
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <numeric>
#include <sstream>
#include <cmath>
#include <set>
#include <stack>
#include <iomanip>
#include <limits.h>
#include <queue>
#include <fstream>
#include <unordered_set>
#include <unordered_map>
#include <exception>
#include <deque>
#include <string>

using namespace std;
fstream in_file;
fstream out_file;

#define BigPrime 786433

vector<int> hashMatrix[BigPrime];

vector<int>::iterator find(int x)
{
    int hasValue = x % BigPrime;

    vector<int>::iterator iterator;

    for (iterator = hashMatrix[hasValue].begin(); iterator != hashMatrix[hasValue].end(); iterator++)
    {
        if (*iterator == x)
            return iterator;
    }
    return hashMatrix[hasValue].end();
}


int main()
{
    in_file.open("hashuri.in");
    out_file.open("hashuri.out", ios::out);

    int n;
    in_file >> n;

    int operatie, val, hashValue;

    while (n--)
    {
        in_file >> operatie;

        if (operatie == 1)
        {
            in_file >> val;
            hashValue = val % BigPrime;
            //Daca nu gasim numarul in hash il adaugam
            if (find(val) == hashMatrix[hashValue].end())
                hashMatrix[hashValue].push_back(val);
        }
        else if (operatie == 2)
        {
            in_file >> val;
            hashValue = val % BigPrime;
            vector<int>::iterator iterator = find(val);
            //Daca gasim numarul in hash il stergem
            if (iterator != hashMatrix[hashValue].end())
                hashMatrix[hashValue].erase(iterator);
        }
        else
        {
            in_file >> val;
            hashValue = val % BigPrime;
            if (find(val) != hashMatrix[hashValue].end())
            {
                out_file << '1' << "\n";
            }
            else
            {
                out_file << '0' << "\n";
            }
        }
    }

    in_file.close();
    out_file.close();
}