Cod sursa(job #2288064)

Utilizator ajeccAjechiloae Eugen ajecc Data 22 noiembrie 2018 20:25:52
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.49 kb
#include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define index INDEX
#ifndef ONLINE_JUDGE
#define print(x) PRINT(x, #x)
template<typename T> inline const void PRINT(T VARIABLE, string NAME) {
    cerr << NAME << " = " << fixed << VARIABLE << '\n';
}
#else
#define print(x) 0
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll INFLL = 2 * (ll)1e18 + 100;
const int INFINT = 2 * (int)1e9 + 100;
const double PI = atan(1) * 4;
const double EPS = 1e-6;
const int SEED = 1e3 + 7;
const int MOD = 1e9 + 7;
const int NMAX = 1e6 + 5;

	
using namespace std;
	
 
	
#define CC_HASH_TABLE_MAX_SIZE 1000003
	
 
	
#define hash __HASH__
	
#define GET_16_BITS(d)(*((const unsigned short *) (d)))
	
static int HtHashFunction(char *Key)
	
{
	
    // Paul Hsieh super fast hash
	
    // Note that I have no idea how this works, but it should avoid collision very well
	
 
	
    if (NULL == Key)
	
    {
	
        perror("ERROR in HtHashFunction => check params: ");
	
        return -1;
	
    }
	
 
	
    char *key = Key;
	
 
	
    unsigned int len = strlen(key);
	
    unsigned int hash = len;
	
    unsigned int rem = len & 3;
	
    len >>= 2;
	
 
	
    while (len--)
	
    {
	
        hash += GET_16_BITS(key);
	
        unsigned int temp = (GET_16_BITS(key + 2) << 11) ^ hash;
	
        hash = (hash << 16) ^ temp;
	
        key += 2 * sizeof(unsigned short);
	
        hash += hash >> 11;
	
    }
	
 
	
    // Handle end cases
	
    switch (rem)
	
    {
	
    case 3:
	
        hash += GET_16_BITS(key);
	
        hash ^= hash << 16;
	
        hash ^= key[sizeof(unsigned short)] << 18;
	
        hash += hash >> 1;
	
        break;
	
    case 2:
	
        hash += GET_16_BITS(key);
	
        hash ^= hash << 11;
	
        hash += hash >> 17;
	
        break;
	
    case 1:
	
        hash += *key;
	
        hash ^= hash << 10;
	
        hash += hash >> 1;
	
    default:
	
        break;
	
    }
	
 
	
    // Force "avalanching" of final 127 bits
	
    hash ^= hash << 3;
	
    hash += hash >> 5;
	
    hash ^= hash << 4;
	
    hash += hash >> 17;
	
    hash ^= hash << 25;
	
    hash += hash >> 6;
	
 
	
    return hash % CC_HASH_TABLE_MAX_SIZE;
	
}
	
 
	
int n;
	
vector<char*>  in[CC_HASH_TABLE_MAX_SIZE];
	
 
	
int check_in(int hash, char* nr)
	
{
	
    for(auto i: in[hash]) if(i == nr) return 1;
	
    return 0;
	
}

int32_t main() {
    FASTIO;
    FILE *read = fopen("hashuri.in", "r");
    FILE *write = fopen("hashuri.out", "w");
    fscanf(read, "%d", &n);
    while(n--) {
        int op;
        fscanf(read, "%d", &op);
        char elem[11];
        fgets(elem, sizeof(elem), read);
        int hash = HtHashFunction(elem); 
        if(op == 1) {
            if(!check_in(hash, elem)) {
                in[hash].pb(elem);
            }
        }
        else if(op == 2) {
            if(check_in(hash, elem)) {
in[hash].erase(remove(in[hash].begin(), in[hash].end(), elem), in[hash].end());
            }
        }
        else fprintf(write, "%d\n", check_in(hash, elem));
        



    }








    return 0;
}