Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- using namespace std;
- ifstream fin("multimap.in");
- ofstream fout("multimap.out");
- vector <vector<string>> HashTable(100001, vector <string>(1));
- int GetHash(string key)
- {
- int Hash = 0;
- int p = 39;
- for (size_t i = 0; i < key.size(); i++) {
- int s = 0;
- if (key[i] - 'a' >= 0 && key[i] - 'a' <= 9)
- s = key[i] - 'a';
- else
- s = key[i] - 'A';
- Hash = (Hash + ((s + 1) * p) % 1000000) % 1000000;
- p = (p * 39) % 1000000;
- }
- return Hash;
- }
- void Put(string key, string value)
- {
- int Hash = GetHash(key);
- if (HashTable[Hash].size() == 1 && HashTable[Hash][0] == "")
- {
- HashTable[Hash][0] = value;
- return;
- }
- for (size_t i = 0; i < HashTable[Hash].size(); ++i)
- {
- if (HashTable[Hash][i] == value)
- return;
- }
- HashTable[Hash].push_back(value);
- }
- int Find(int Hash, string value)
- {
- for (size_t i = 0; i < HashTable[Hash].size(); ++i)
- {
- if (HashTable[Hash][i] == value)
- return i;
- }
- }
- void Delete(string key, string value)
- {
- int Hash = GetHash(key);
- int index = Find(Hash, value);
- HashTable[Hash].erase(HashTable[Hash].begin() + index);
- }
- void DeleteAll(string key)
- {
- int Hash = GetHash(key);
- HashTable[Hash].clear();
- HashTable[Hash].push_back("");
- }
- void Get(string key)
- {
- int Hash = GetHash(key);
- if (HashTable[Hash][0] == "")
- {
- cout << "0" << endl;
- return;
- }
- int size = HashTable[Hash].size();
- cout << size << " ";
- for (size_t i = 0; i < HashTable[Hash].size(); ++i)
- cout << HashTable[Hash][i] << " ";
- cout << endl;
- }
- int main()
- {
- string s;
- while (cin >> s)
- {
- if (s == "put")
- {
- string key, value;
- cin >> key >> value;
- Put(key, value);
- }
- if (s == "delete")
- {
- string key, value;
- cin >> key >> value;
- Delete(key, value);
- }
- if (s == "deleteall")
- {
- string key;
- cin >> key;
- DeleteAll(key);
- }
- if (s == "get")
- {
- string key;
- cin >> key;
- Get(key);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement