Cod sursa(job #2206034)

Utilizator RobybrasovRobert Hangu Robybrasov Data 20 mai 2018 21:26:32
Problema Dtcsu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>
#define MOD 39119

#define SMAX 16384
#define N 276997

using namespace std;

struct node {
    uint32_t val;
    node* next;
    node(uint32_t v, node* nn) : val(v), next(nn) {}
};

char s[SMAX];
int p;
FILE *f;
node* H[MOD];

void read(uint64_t &x) {
    x = 0;
    for (; s[p] >= '0' && s[p] <= '9'; ++p) {
        x = 10 * x + s[p] - '0';
        if (p == SMAX - 1) {
            fread(s, 1, SMAX, f);
            p = -1;
        }
    }
    for (; s[p] < '0' || s[p] > '9'; ++p) {
        if (p == SMAX - 1) {
            fread(s, 1, SMAX, f);
            p = -1;
        }
    }
}

inline void insert(uint32_t x) {
    int pos = x % MOD;
    node* it = H[pos];
    for (; it != nullptr && it->val != x; it = it->next);
    if (it == nullptr) {
        node* nn = new node(x, H[pos]);
        H[pos] = nn;
    }
}

inline bool contains(uint32_t x) {
    int pos = x % MOD;
    node* it = H[pos];
    for (; it != nullptr && it->val != x; it = it->next);
    return it != nullptr;
}

int main() {
    f = fopen("dtcsu.in", "r");
    FILE* g = fopen("dtcsu.out", "w");

    hash<uint64_t> hfunc;

    fread(s, 1, SMAX, f);
    p = 0;

    for (int i = 0; i < N; ++i) {
        uint64_t x;
        read(x);
        insert((uint32_t) hfunc(x));
    }
    uint64_t n;
    read(n);
    int k = 0;
    while (n--) {
        uint64_t x;
        read(x);
        if (contains((uint32_t) hfunc(x))) {
            ++k;
        }
    }
    fprintf(g, "%d\n", k);

    return 0;
}