Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <string>
- #include <set>
- #include <stack>
- #include <queue>
- #include <deque>
- using namespace std;
- #define TASK "multimap"
- const int sz1 = 500003, sz2 = 289, tt = 211;
- struct hshmap {
- string s;
- int sz;
- pair < string, bool > hm[sz2];
- };
- vector < hshmap > hash_map[sz1];
- string key, zn, a;
- int hash_f1(string s) {
- int p = 449;
- int hs = 0;
- for (int i = 0; i < s.size(); ++i) {
- hs = hs * p + s[i];
- if (hs >= sz1) {
- hs %= sz1;
- }
- }
- return hs;
- }
- int hash_f2(string s) {
- int p = 457;
- int hs = 0;
- for (int i = 0; i < s.size(); ++i) {
- hs = hs * p + s[i];
- if (hs >= sz2) {
- hs %= sz2;
- }
- }
- return hs;
- }
- int hash_f(string s) {
- int p = 487;
- unsigned long long hs = 0;
- for (int i = 0; i < s.size(); ++i) {
- hs = hs * p + s[i];
- }
- return hs;
- }
- void get() {
- int hk = hash_f1(key);
- unsigned long long hk2 = hash_f(key);
- for (int i = 0; i < hash_map[hk].size(); ++i) {
- if (hash_f(hash_map[hk][i].s) == hk2) {
- cout << hash_map[hk][i].sz << " ";
- for (int j = 0; j < sz2; ++j) {
- if (hash_map[hk][i].hm[j].second) {
- cout << hash_map[hk][i].hm[j].first << " ";
- }
- }
- cout << "\n";
- return;
- }
- }
- cout << "0 \n";
- }
- void put() {
- int hk = hash_f1(key), hs = hash_f2(zn), t = -1, j;
- unsigned long long hk2 = hash_f(key), hs2 = hash_f(zn);
- j = hs;
- for (int i = 0; i < hash_map[hk].size(); ++i) {
- if (hash_f(hash_map[hk][i].s) == hk2) {
- do {
- if (hash_f(hash_map[hk][i].hm[j].first) == hs2) {
- if (!hash_map[hk][i].hm[j].second) {
- hash_map[hk][i].hm[j].second = true;
- hash_map[hk][i].sz++;
- }
- return;
- }
- if (!hash_map[hk][i].hm[j].second) {
- t = j;
- }
- j = (j + tt) % sz2;
- } while (j != hs);
- hash_map[hk][i].hm[t] = make_pair(zn, true);
- hash_map[hk][i].sz++;
- return;
- }
- }
- hshmap a;
- a.s = key;
- a.sz = 1;
- a.hm[hs] = make_pair(zn, true);
- hash_map[hk].push_back(a);
- }
- void del() {
- int hk = hash_f1(key), hs = hash_f2(zn), j;
- unsigned long long hk2 = hash_f(key), hs2 = hash_f(zn);
- j = hs;
- for (int i = 0; i < hash_map[hk].size(); ++i) {
- if (hash_f(hash_map[hk][i].s) == hk2) {
- do {
- if (hash_f(hash_map[hk][i].hm[j].first) == hs2 && hash_map[hk][i].hm[j].second) {
- hash_map[hk][i].hm[j].second = false;
- hash_map[hk][i].sz--;
- }
- j = (j + tt) % sz2;
- } while (j != hs);
- return;
- }
- }
- }
- void delall() {
- int hk = hash_f1(key);
- unsigned long long hk2 = hash_f(key);
- hshmap a;
- a.s = ""; a.sz = 0;
- for (int i = 0; i < hash_map[hk].size(); ++i) {
- if (hash_f(hash_map[hk][i].s) == hk2) {
- hash_map[hk][i] = a;
- return;
- }
- }
- }
- int main() {
- #ifdef _DEBUG
- freopen("debug.in", "r", stdin);
- freopen("debug.out", "w", stdout);
- #else
- freopen(TASK".in", "r", stdin);
- freopen(TASK".out", "w", stdout);
- #endif // _DEBUG
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- while (cin >> a) {
- cin >> key;
- if (a[0] == 'p') {
- cin >> zn;
- put();
- }
- if (a[0] == 'g') {
- get();
- }
- if (a[0] == 'd') {
- if (a.size() == 6) {
- cin >> zn;
- del();
- }
- else {
- delall();
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement