Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- #define TASKNAME "multimap"
- using namespace std;
- bool prime(int x) {
- for(int i = 2; i * i <= x; i ++)
- if(x % i == 0)
- return 0;
- return 1;
- }
- int getSmCode(char x) {
- if(x >= 'a' && x <= 'z')
- return x - 'a' + 1;
- return x - 'A' + 1 + 26;
- }
- struct HashSet{
- int mod = 541, cnt = 0;
- vector<string> arr[541];
- HashSet() {
- for(int i = 0; i < 541; i ++)
- arr[i] = {};
- }
- int count() {
- return this -> cnt;
- }
- int getHash(string x) {
- int cp = 1;
- int res = 0;
- for(auto i : x) {
- res += (getSmCode(i) * cp) % mod;
- cp *= 59;
- cp %= mod;
- res %= mod;
- }
- return res % mod;
- }
- bool exists(string x) {
- int hash = getHash(x);
- for(auto i : arr[hash])
- if(i == x)
- return 1;
- return 0;
- }
- void insert(string x) {
- if(exists(x))
- return;
- cnt++;
- arr[getHash(x)].push_back(x);
- }
- void print() {
- cout << cnt;
- for(int i = 0; i < mod; i ++)
- for(auto j : arr[i])
- cout << ' ' << j;
- cout << "\n";
- }
- void remove(string x) {
- if(!exists(x)) return;
- cnt--;
- int h = getHash(x);
- int idx = 0;
- int n = arr[h].size();
- for(int i = 0; i < n; i ++)
- if(arr[h][i] == x)
- idx = i;
- swap(arr[h][idx], arr[h][arr[h].size() - 1]);
- arr[h].pop_back();
- }
- };
- struct HashMap{
- int mod = 1e6 + 33;
- vector<pair<string, HashSet*> > arr[(int)(1e6 + 33)];
- HashMap() {
- mod = 1e6+33;
- for(int i = 0; i < mod; i ++)
- arr[i] = {};
- }
- int getHash(string x) {
- int cp = 1;
- int res = 0;
- for(auto i : x) {
- res += (i * cp) % mod;
- cp *= 137;
- cp %= mod;
- res %= mod;
- }
- return res % mod;
- }
- void put(string x, string y) {
- if(!exists(x))
- {
- arr[getHash(x)].push_back({x, new HashSet()});
- arr[getHash(x)].back().second -> insert(y);
- return;
- }
- int hash = getHash(x), idx = 0;
- for(int i = 0; i < arr[hash].size(); i ++)
- if(arr[hash][i].first == x)
- arr[hash][i].second -> insert(y);
- }
- bool exists(string x) {
- int hash = getHash(x);
- for(int i = 0; i < arr[hash].size(); i++)
- if(arr[hash][i].first == x)
- return 1;
- return 0;
- }
- void get(string x) {
- if(!exists(x))
- {
- cout << "0\n";
- return;
- }
- int hash = getHash(x);
- for(int i = 0; i < arr[hash].size(); i ++)
- if(arr[hash][i].first == x)
- arr[hash][i].second -> print();
- }
- void remove(string x, string y) {
- if(!exists(x)) return;
- int hash = getHash(x), idx = -1;
- for(int i = 0; i < arr[hash].size(); i ++)
- if(arr[hash][i].first == x)
- idx = i;
- if(idx == -1) return;
- arr[hash][idx].second -> remove(y);
- if(arr[hash][idx].second -> count() == 0)
- remove_all(x);
- }
- void remove_all(string x) {
- if(!exists(x)) return;
- int idx = -1, hash = getHash(x);
- for(int i = 0; i < arr[hash].size(); i ++)
- if(arr[hash][i].first == x)
- idx = i;
- if(idx == -1) return;
- if(arr[hash].size() == 0) return;
- swap(arr[hash][idx], arr[hash][arr[hash].size() - 1]);
- arr[hash].pop_back();
- }
- };
- HashMap* hm;
- int32_t main() {
- srand(time(NULL));
- ios_base::sync_with_stdio(0);
- freopen(TASKNAME".in", "r", stdin);
- freopen(TASKNAME".out", "w", stdout);
- cin.tie(NULL);
- cout.tie(NULL);
- string s;
- string x, y;
- hm = new HashMap();
- while(cin >> s) {
- cin >> x;
- if(s == "put")
- {
- cin >> y;
- hm -> put(x, y);
- }
- else if(s == "get")
- hm -> get(x);
- else if(s == "delete")
- {
- cin >> y;
- hm -> remove(x, y);
- } else if(s == "deleteall")
- hm -> remove_all(x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement