Cod sursa(job #1357939)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 24 februarie 2015 11:13:42
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
// Acest cod este facut sa fie cat mai scurt, in limita restrictiilor de coding-style. De aceeea el foloseste vector<> si break
#include <fstream>
#include <vector>
#define MODULUS 666013
using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
 
vector<int> tab[MODULUS];

inline unsigned int H(int k)
{
    unsigned int hash = 0;
 
        hash += k;
        hash += (hash << 10);
        hash ^= (hash >> 6);
        
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash % MODULUS;
}
 
void push(int val)
{
    int h = H(val);
    int flag = 1;
    for (int i = 0; i < tab[h].size(); ++i) {
        if (tab[h][i] == val) {
            flag = 0;
        }
    }
    if (flag) {
        tab[h].push_back(val);
    }
}
 
void pop(int val)
{
    int h = H(val, 1);
    for (int i = 0; i < tab[h].size(); ++i) {
        if (tab[h][i] == val) {
            tab[h].erase(tab[h].begin() + i);
            break;
        }
    }
}
 
bool check(int val)
{
    int h = H(val, 1);
    for (int i = 0; i < tab[h].size(); ++i) {
        if (tab[h][i] == val) {
            return 1;
        }
    }
    return 0;
}
 
int main()
{
    int N;
    fin>>N;
    for (int i = 0; i < N; ++i) {
        int op, num;
        fin>>op>>num;
        if (op == 1) {
            push(num);
        } else if (op == 2) {
            pop(num);
        } else if (op == 3) {
            fout<<(check(num) ? "1\n" : "0\n");
        }
    }
}