Cod sursa(job #2287523)

Utilizator valorosu_300Cristian Gherman valorosu_300 Data 21 noiembrie 2018 23:31:00
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
const int MOD = 666013;

vector <int> h[MOD]; /// hash

int convert(int val){
    return val % MOD;
}

int search(int key, int val){
    ///checks if val already exists in the hash table
    ///-1 means that the value wasn't found
    ///any other value is the position of the value
    int sz = h[key].size();
    for(int i=0;i<sz;i++)
        if(h[key][i] == val)
            return i;
    return -1;
}

void insertValue(int val){
    int key = convert(val);
    int address = search(key,val);
    if(address == -1)
        h[key].push_back(val);
}

void deleteValue(int val){
    int key = convert(val);
    int address = search(key,val);
    if(address != -1)
        h[key].erase(h[key].begin() + address);
}

bool findValue(int val){
    int key = convert(val);
    int pos = search(key,val);
    if(pos == -1)
        return false;
    return true;
}

int main()
{
    int n, q, val;
    in>>n;
    for(int i=1;i<=n;i++){
        in>>q>>val;
        if(q == 1)
            insertValue(val);
        else if(q == 2)
            deleteValue(val);
        else
            out<<findValue(val)<<"\n";
    }
    in.close();
    out.close();
    return 0;
}