Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- //ifstream fin("multimap.txt");
- //ofstream fout("multimap2.txt");
- struct item {
- string key;
- vector <string> val;
- };
- vector<vector<item>> HashTable(10000001, vector<item>());
- int HashKey(string key) {
- int sum = 0;
- for (int i = 0; i < key.size(); i++)
- sum = sum * 13 + (int) key[i];
- return abs(sum % 100003);
- }
- bool ExistsString(string key) {
- int hash = HashKey(key);
- for (int i = 0; i < HashTable[hash].size(); i++)
- if (HashTable[hash][i].key == key)
- return true;
- return false;
- }
- string GetString(string key) {
- int hash = HashKey(key);
- for (int i = 0; i < HashTable[hash].size(); i++)
- if (ExistsString(key))
- if (HashTable[hash][i].key == key) {
- cout << HashTable[hash][i].val.size() << " ";
- for (int j = 0; j < HashTable[hash][i].val.size(); j++)
- cout << HashTable[hash][i].val[j] << " ";
- }
- else
- cout << 0;
- }
- void PutString(string key, string value) {
- int hash = HashKey(key);
- int flag = 0;
- for (int i = 0; i < HashTable[hash].size(); i++)
- if (HashTable[hash][i].key == key) {
- for (int j = 0; j < HashTable[hash][i].val.size(); j++)
- if (HashTable[hash][i].val[j] == value) {
- flag = 1;
- return;
- }
- if (flag == 0) {
- flag = 2;
- HashTable[hash][i].val.push_back(value);
- }
- }
- if (flag == 0) {
- HashTable[hash].push_back({key, { value }});
- }
- }
- void DeleteString(string key, string value) {
- int hash = HashKey(key);
- for (int i = 0; i < HashTable[hash].size(); i++)
- if (HashTable[hash][i].key == key)
- for (int j = 0; j < HashTable[hash][i].val.size(); j++)
- if (HashTable[hash][i].val[j] == value)
- HashTable[hash][i].val.erase(HashTable[hash][i].val.begin() + i);
- }
- void DeleteAllString(string key) {
- int hash = HashKey(key);
- for (int i = 0; i < HashTable[hash].size(); i++)
- if (HashTable[hash][i].key == key) {
- for (int j = 0; j < HashTable[hash][i].val.size(); j++)
- HashTable[hash][i].val.erase(HashTable[hash][i].val.begin() + i);
- HashTable[hash][i].key.erase(HashTable[hash][i].key.begin() + i);
- }
- }
- int main() {
- string operation;
- string str, value;
- while (cin >> operation) {
- if (operation == "put") {
- cin >> str >> value;
- PutString(str, value);
- }
- if (operation == "delete") {
- cin >> str >> value;
- DeleteString(str, value);
- }
- if (operation == "get") {
- cin >> str;
- GetString(str);
- cout << "\n";
- }
- if (operation == "deleteall") {
- cin >> str;
- DeleteAllString(str);
- }
- }
- //fin.close();
- //fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement