int n; constexprint N = 100003; int h[N], e[N], ne[N], idx;
intcreate_hash(int x) { return (x % N + N) % N; }
voidinsert(int x) { int hash_val = create_hash(x); // inserts to linked list head int head = h[hash_val]; e[idx] = x; ne[idx] = head; // updates head h[hash_val] = idx; ++idx; }
boolexist(int x) { int hash_val = create_hash(x); for (int ptr = h[hash_val]; ptr != __nullptr__; ptr = ne[ptr]) { // exist entry in linked list if (e[ptr] == x) returntrue; } // no entry in linked list or no value equals to x although x has the same hash val with each entry in linked list returnfalse; }
intmain() { string ops; int x; cin >> n; // inits nullptr, using -1 as nullptr memset(h, __nullptr__, sizeof(h)); memset(ne, __nullptr__, sizeof(ne)); for (; n--;) { cin >> ops >> x; if (ops == "I") { insert(x); } elseif (ops == "Q") { int res = exist(x); if (res) cout << "Yes" << endl; elsecout << "No" << endl; } else { cout << "UNSUPPORTED OPERATION" << endl; } } }
// returns the hash index of given value if value not in set // or occupied location if in set intfind(int x) { int hash_val = create_hash(x); for (; h[hash_val] != __unreachable__ && h[hash_val] != x ; ++hash_val) { if (hash_val == N) hash_val = 0; } return hash_val; }
intmain() { string ops; int x; cin >> n; // inits h with each val equals to unreachable value memset(h, 0x3f, sizeof(h)); for (; n--;) { cin >> ops >> x; int hash_val = find(x); if (ops == "I") { h[hash_val] = x; } elseif (ops == "Q") { if (h[hash_val] == __unreachable__) cout << "No" << endl; elsecout << "Yes" << endl; } else { cout << "UNSUPPORTED OPERATION" << endl; } } }