Cod sursa(job #1147548)

Utilizator mucenic_b101Bogdan Mucenic mucenic_b101 Data 19 martie 2014 22:27:28
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <cstdio>
#include <vector>
#define MOD 500007
using namespace std;

struct Hash {
    vector <int> V[MOD];
    bool find( int x ) {
        int key = x % MOD;
        int poz = 0;
        while( poz < V[key].size() && V[key][poz] != x )
            ++poz;
        if( poz < V[key].size() )
            return true;
        return false;
    }

    void add( int x ) {
        V[x%MOD].push_back( x );
    }
    void erase( int x ) {
        int key = x % MOD, poz = 0;
        while( V[key][poz] != x )
            ++poz;
        V[key][poz] = V[key][V[key].size()-1];
        V[key].pop_back();
    }
};

Hash H;

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

    int n, op, x;

    fscanf( f, "%d", &n );

    for( int i = 0 ; i < n ; ++i ) {
        fscanf( f, "%d%d", &op, &x );
        switch( op ) {
            case 1:
                H.add( x );
                break;
            case 2:
                if( H.find( x ) )
                    H.erase( x );
                break;
            case 3:
                fprintf( g, "%d\n", H.find( x ) );
                break;
        }
    }

    fclose( f );
    fclose( g );
    return 0;
}