Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define SIZE_H1 (int)1e+4 + 7
- #define SIZE_H2 (int)1e+2 + 9
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- struct Pair {
- string key;
- vector < vector <string> > val;
- };
- vector <vector <Pair> > hash_t(SIZE_H1);
- int hashFunc(string key, int size) {
- int p = 137;
- int p_pow = 1;
- int hash = 0;
- for (size_t i = 0; i < key.size(); i++) {
- hash += (int)key[i] * p_pow;
- p_pow *= p; p_pow %= size;
- hash %= size;
- }
- return abs(hash);
- }
- void putPair(string key, string val);
- void deletePair(string key, string val);
- void deleteAll(string key);
- void getKey(string key);
- int main() {
- ios_base::sync_with_stdio(false);
- freopen("multimap.in", "r", stdin);
- freopen("multimap.out", "w", stdout);
- string operation, key, value;
- while (cin >> operation) {
- cin >> key;
- if (operation == "put") {
- cin >> value;
- putPair(key, value);
- }
- else if (operation == "delete") {
- cin >> value;
- deletePair(key, value);
- }
- else if (operation == "deleteall")
- deleteAll(key);
- else if (operation == "get")
- getKey(key);
- }
- return 0;
- }
- void putPair(string key, string val) {
- int hash1 = hashFunc(key, SIZE_H1);
- int hash2 = hashFunc(val, SIZE_H2);
- for (size_t i = 0; i < hash_t[hash1].size(); i++) {
- if (hash_t[hash1][i].key == key) {
- auto exs = find(hash_t[hash1][i].val[hash2].begin(), hash_t[hash1][i].val[hash2].end(), val);
- if (exs == hash_t[hash1][i].val[hash2].end())
- hash_t[hash1][i].val[hash2].push_back(val);
- return;
- }
- }
- Pair *p = new Pair;;
- p->key = key;
- p->val.resize(SIZE_H2);
- p->val[hash2].push_back(val);
- hash_t[hash1].push_back(*p);
- delete p;
- }
- void deletePair(string key, string val) {
- int hash1 = hashFunc(key, SIZE_H1);
- int hash2 = hashFunc(val, SIZE_H2);
- for (size_t i = 0; i < hash_t[hash1].size(); i++)
- if (hash_t[hash1][i].key == key)
- for (size_t j = 0; j < hash_t[hash1][i].val[hash2].size(); j++)
- if (hash_t[hash1][i].val[hash2][j] == val) {
- swap(hash_t[hash1][i].val[hash2][j], hash_t[hash1][i].val[hash2].back());
- hash_t[hash1][i].val[hash2].pop_back();
- }
- }
- void deleteAll(string key) {
- int hash1 = hashFunc(key, SIZE_H1);
- for (size_t i = 0; i < hash_t[hash1].size(); i++)
- if (hash_t[hash1][i].key == key) {
- hash_t[hash1][i] = hash_t[hash1].back();
- hash_t[hash1].pop_back();
- }
- }
- void getKey(string key) {
- int hash1 = hashFunc(key, SIZE_H1);
- vector <string> buffer;
- for (size_t i = 0; i < hash_t[hash1].size(); i++)
- if (hash_t[hash1][i].key == key) {
- for (size_t j = 0; j < SIZE_H2; j++)
- for (size_t z = 0; z < hash_t[hash1][i].val[j].size(); z++)
- buffer.push_back(hash_t[hash1][i].val[j][z]);
- cout << buffer.size() << " ";
- for (size_t k = 0; k < buffer.size(); k++)
- cout << buffer[k] << " ";
- cout << endl;
- return;
- }
- cout << 0 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement