Cod sursa(job #1017877)

Utilizator bghimisFMI Ghimis Bogdan bghimis Data 28 octombrie 2013 16:26:04
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

vector<int> hashu[886381];

int hashFunc (int nr)
{
  return nr % 886381;
}

int cauta(int n);

void adaugare (int val)
{
  // <----- ce tampenie ------>
  if (cauta(val) == 1)
    return;

  hashu[hashFunc(val)].push_back(val);
}

void stergere (int val)
{
  int hashed = hashFunc(val);

  for (vector<int>::iterator i = hashu[hashed].begin(); i != hashu[hashed].end(); ++i)
    if (*i == val)
      {
	hashu[hashed].erase(i);
	return;
      }
}

int cauta(int val)
{
  int hashed = hashFunc(val);
  for (vector<int>::iterator i = hashu[hashed].begin(); i != hashu[hashed].end(); ++i)
    if (*i == val)
      return 1;

  return 0;
}

int main()
{
  FILE *fin, *fout;
  
  fin = fopen( "hashuri.in", "r" );
  int q;

  fscanf( fin, "%d", &q );
  
  fout = fopen( "hashuri.out", "w" );
  while ( q ) {
    int type, val;

    fscanf( fin, "%d%d", &type, &val );
      
    if (type == 1)
      adaugare(val);
    else if (type == 2)
      stergere(val);
        else
	  fprintf( fout, "%d\n", cauta(val) ? 1 : 0 );
    
    --q;
    }
  
  fclose( fin );
  fclose( fout );
  
  return 0;
}