Cod sursa(job #2544131)

Utilizator GeorgianBaditaBadita Marin-Georgian GeorgianBadita Data 11 februarie 2020 19:51:02
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.63 kb
#include<bits/stdc++.h>
#define P 666013


//in-out
std::ifstream f("hashuri.in");
std::ofstream g("hashuri.out");

int n;

//data
class Node{
private:
    int value;
    Node* next;
public:
    Node(int value): value(value){
        next = nullptr;
    }

    int getValue() {return this->value;}
    Node* getNext() {return this->next;}

    void setValue(int value) {this->value = value;}
    void setNext(Node* next) {this->next = next;}
};

class List{
private:
    Node* head;
public:
    List(){
        head = nullptr;
    }

    void add(int x){
        auto node = new Node(x);
        node->setNext(head);
        head = node;
        
    }

    void deleteNode(int x){
        //[] -> [] -> [] -> []
        if(head == nullptr){
            return;
        }
        if(head->getValue() == x){
            head = head->getNext();
            return;
        }
        if(head->getNext() == nullptr){
            return;
        }
        auto tmp = head;
        auto before = head;
        tmp = tmp->getNext();
        while(tmp != nullptr && tmp->getValue() != x){
            before = tmp;
            tmp = tmp->getNext();
        } 
        if(tmp == nullptr){
            return;
        }
        before->setNext(tmp->getNext());
        delete tmp;
    }

    bool exists(int x){
        auto tmp = head;
        while(tmp != nullptr){
            if(tmp->getValue() == x){
                return true;
            }
            tmp = tmp->getNext();
        }
        return false;
    }

    void printLst(){
        auto tmp = head;
        while(tmp != nullptr){
            std::cout << tmp->getValue() << ' ';
            tmp = tmp->getNext();
        }
    }

    ~List(){
        auto tmp = head;
        while(tmp != nullptr){
            auto curr = tmp;
            tmp = tmp->getNext();
            delete curr;
        }
    }
};

class Hash{
private:
    int dim;
    List* hash;
public:
    Hash(int dim): dim(dim){
        hash = new List[dim];
    }

    int getDim() {return this->dim;}

    void insert(int x){
        int i = x % dim;
        hash[i].add(x);
    }

    void deleteElem(int x){
        int i = x % dim;
        hash[i].deleteNode(x);
    }

    bool exists(int x){
        int i  = x % dim;
        return hash[i].exists(x); 
    }

    ~Hash(){
        delete[] hash;
    }

};
Hash H = Hash(P);


//soolve
void solve(){
    f >> n;
    int op, param;
    for(int i = 0; i<n; i++){
        f >> op >> param;
        if(op == 1){
            H.insert(param);
        }else if(op == 2){
            H.deleteElem(param);
        }else{
            g << H.exists(param) << '\n';
        }
    }
}

int main(){
    solve();
    return 0;
}