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;
- 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);
- for (size_t i = 0; i < HashTable[Hash].size(); ++i)
- {
- if (HashTable[Hash][i] == value)
- return;
- HashTable[Hash].push_back(value);
- return;
- }
- cout << HashTable[Hash].capacity();
- }
- 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();
- }
- void Get(string key)
- {
- int Hash = GetHash(key);
- cout << HashTable[Hash].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