Cod sursa(job #3277805)

Utilizator schema_227Stefan Nicola schema_227 Data 17 februarie 2025 13:52:40
Problema Potrivirea sirurilor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>

using namespace std;

ifstream in("hashuri.in");
ofstream out("hashuri.out");

const int MOD = 666103;
const int BASE = 31;

int hashBaza31(int x) {
    long long hash_value = 0;
    string key = to_string(x);
    for (char ch : key) {
        hash_value = (hash_value * BASE + ch) % MOD;
    }
    return (int)hash_value;
}

int main()
{
    int n;
    in >> n;

    vector<list<int>> hash_table(MOD);

    while(n--){
        int op, x;
        in >> op >> x;

        int hash_value = hashBaza31(x);

        if(op == 1){
            hash_table[hash_value].push_back(x);
        }
        if(op == 2){
            auto &bucket = hash_table[hash_value];
            auto it = find(bucket.begin(), bucket.end(), x);
            if(it != bucket.end()) {
                bucket.erase(it);
            }
        }
        if(op == 3){
            auto &bucket = hash_table[hash_value];
            bool found = find(bucket.begin(), bucket.end(), x) != bucket.end();
            out << (found ? 1 : 0) << '\n';
        }
    }
    return 0;
}