Cod sursa(job #2795739)

Utilizator LucaMihaiLM10Luca Ilie LucaMihaiLM10 Data 6 noiembrie 2021 21:17:14
Problema Dtcsu Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <stdio.h>
#include <vector>
#include <ctype.h>

#define DTCSU 276997
#define HASH_SIZE 9973

using namespace std;

vector <long long> hashTable[HASH_SIZE];

static inline int computeHash( long long x ) {
    if ( x < 0 )
        return 0;
    return x % HASH_SIZE;
}

bool searchHashTable( long long x ) {
    int hashCode, n, i;

    hashCode = computeHash( x );
    n = hashTable[hashCode].size();

    i = 0;
    while ( i < n && hashTable[hashCode][i] != x )
        i++;

    return (i < n);
}

void addHashTable( long long x ) {
    int hashCode, n, i;

    hashCode = computeHash( x );
    n = hashTable[hashCode].size();

    i = 0;
    while ( i < n && hashTable[hashCode][i] != x )
        i++;

    if ( i == n )
        hashTable[hashCode].push_back( x );
}

long long getNum() {
    char ch;
    long long n;

    ch = getc( stdin );
    while ( isspace( ch ) && ch != EOF )
        ch = getc( stdin );

    n = 0;
    while ( isdigit( ch ) ) {
        n = n * 10 + ch - '0';
        ch = getc( stdin );
    }

    return n;
}

int main() {
    FILE *fin, *fout;
    int n, i;
    long long a;

    fin = fopen( "dtcsu.in", "r" );

    for ( i = 0; i < DTCSU; i++ ) {
        a = getNum();
        addHashTable( a );
    }

    fout = fopen( "dtcsu.out", "w" );

    n = getNum();
    for ( i = 0; i < n; i++ ) {
        a = getNum();
        fprintf( fout, "%d\n", searchHashTable( a ) );
    }

    fclose( fin );
    fclose( fout );

    return 0;
}