Cod sursa(job #2649689)

Utilizator zarg169Roxana zarg169 Data 15 septembrie 2020 19:59:19
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
int modulo = 1000000;
vector<int> hashtable[1000000];

void add(int x) {
    int key = x % modulo;
    int c = 0;
    for (int j = 0; j < hashtable[key].size(); ++j) {
         if (x != hashtable[key][j]) {
             c += 0;
         } else {
             c += 1;
         }
    }
    if (c == 0) {
        hashtable[key].push_back(x);
    }
}

void deleteValue(int x) {
    int key = x % modulo;
    for (int j = 0; j < hashtable[key].size(); ++j) {
        if (x == hashtable[key][j]) {
            swap(hashtable[key][j], hashtable[key].back());
                 hashtable[key].pop_back();
        }
    }
}

int returnValue(int x) {
    int key = x % modulo;
    int answer = 0;
    for (int j = 0; j < hashtable[key].size(); ++j) {
         if (x == hashtable[key][j]) {
             answer = 1;
             return answer;
             break;
         } else {
              answer = 0;
         }
    }
    return answer;
}


int main()
{
    ifstream fin("hashuri.in");
    ofstream fout("hashuri.out");

    int n;
    fin >> n;
    for (int i = 0; i <= n - 1; ++i) {
        int op, x;
        fin >> op >> x;

        if (op == 1) {
            add(x);
        }
        if (op == 2) {
            deleteValue(x);
        }
        if (op == 3) {
            fout << returnValue(x);
            fout << "\n";
        }
    }

    return 0;
}