Cod sursa(job #2670724)

Utilizator teodorescunicolasteodorescu nicolas alexandru teodorescunicolas Data 10 noiembrie 2020 16:32:16
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <stdio.h>
#include <vector>
#include <ctype.h>

using namespace std;
#define MOD 666013

FILE *fin, *fout;
vector<int> myHash[MOD];

int readInt() {
    int numar = 0;
    char ch;
    while ( isspace( ch = fgetc( fin ) ) );
    do {
        numar = numar * 10 + ch - '0';
    } while ( isdigit( ch = fgetc( fin ) ) );
    return numar;
}
bool cauta( int x ) {
    int cod = x % MOD;
    unsigned int i = 0;
    while ( i < myHash[cod].size() && myHash[cod][i] != x ) {
        i++;
    }
    return i < myHash[cod].size();
}

void adauga( int x ) {
    int cod = x % MOD;
    unsigned int i = 0;
    while ( i < myHash[cod].size() && myHash[cod][i] != x ) {
        i++;
    }
    if ( i == myHash[cod].size() ) {
        myHash[cod].push_back( x );
    }
}

void sterge( int x ) {
    int cod = x % MOD;
    unsigned int i = 0;
    while ( i < myHash[cod].size() && myHash[cod][i] != x ) {
        i++;
    }
    if ( i < myHash[cod].size() ) {
        myHash[cod].erase( myHash[cod].begin() + i );
    }
}

int main()
{
    int n, cer, i, x;
    fin = fopen( "hashuri.in", "r" );
    fout = fopen( "hashuri.out", "w" );
    fscanf( fin, "%d", &n );
    for ( i = 0; i < n; i++ ) {
        cer = readInt();
        x = readInt();
        if ( cer == 1 ) {
            adauga( x );
        } else if ( cer == 2 ) {
            sterge( x );
        } else {
            fprintf( fout, "%d\n", cauta( x ) );
        }
    }
    fclose( fin );
    fclose( fout );
    return 0;
}