Cod sursa(job #759477)

Utilizator test13test13 test13 Data 18 iunie 2012 12:11:56
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define LIM 666013

vector<int>h[LIM];
vector<int>::iterator it;

int hash(int x){
    int sum = 0;
    while(x>0){ sum += (LIM*(x%10))>>1; x/=10; }
    return sum%LIM;
}

bool find(int x){
    int p=hash(x);
    for(int i=0;i<h[p].size();i++)
    if(h[p][i]==x)return 1;
    return 0;
}

void insert(int x){
    int p=hash(x);
    if(!find(x))h[p].push_back(x);
}

void erase(int x){
    int p=hash(x);
    it=h[p].begin();
    for(;it!=h[p].end();it++)
    if(*it==x)
    {
        h[p].erase(it);
        return;
    }
}

int main(){
    int m,c,x;
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d %d",&c,&x);
            switch(c)
            {
                case 1: insert(x); break;
                case 2: erase(x); break;
                case 3: printf("%d\n",find(x)); break;
            }
        }

    return 0;
}