Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int N = 1e5;
- const int Y = 1e5;
- const int lul = 31;
- int arr[N];
- struct seet{
- string key;
- vector <vector<string>> mas;
- seet(){
- mas.resize(228);
- }
- };
- struct myltimap{
- vector <vector<seet>> set;
- myltimap(){
- set.resize(14880);
- }
- };
- myltimap map;
- vector <string> memes;
- void module() {
- arr[0] = 1;
- for (int i = 1 ; i < N; ++ i) {
- arr[i] = arr[i - 1] * lul;
- arr[i] = abs(arr[i]);
- arr[i] %= Y;
- }
- }
- int get_hash(string key, int size) {
- int hash_x = 0;
- for(char i : key) {
- hash_x = 41 * hash_x + i;
- }
- return abs(hash_x % size);
- }
- void put(string key,string x){
- int hash_key = get_hash(key, 14880);
- for (auto & i : map.set[hash_key]){
- if (i.key == key){
- int elHash = get_hash(x, 228);
- for (const auto & luls : i.mas[elHash]){
- if (luls == x)
- return;
- }
- i.mas[elHash].push_back(x);
- return;
- }
- }
- auto* temp_set = new seet;
- temp_set->key = key;
- temp_set->mas[get_hash(x, 228)].push_back(x);
- map.set[hash_key].push_back(*temp_set);
- delete temp_set;
- }
- void del(string key, string x){
- int hash_key = get_hash(key, 14880);
- for (auto & i : map.set[hash_key]){
- if (i.key == key){
- int hash_x = get_hash(x, 228);
- for (int luls = 0; luls < i.mas[hash_x].size(); luls++){
- if (i.mas[hash_x][luls] == x) {
- swap(i.mas[hash_x][luls], i.mas[hash_x].back());
- i.mas[hash_x].pop_back();
- return;
- }
- }
- }
- }
- }
- void del_all(string key){
- int hash_key = get_hash(key, 14880);
- for (auto & i : map.set[hash_key]){
- if (i.key == key){
- for(auto & luls : i.mas){
- while(!luls.empty())
- luls.pop_back();
- }
- return;
- }
- }
- }
- void get (string key){
- int hash_key = get_hash(key, 14880);
- for (auto & i : map.set[hash_key]){
- if (i.key == key){
- for(auto & luls : i.mas){
- for(const auto & k : luls) {
- memes.push_back(k);
- }
- }
- return;
- }
- }
- }
- int main(){
- freopen("multimap.in", "r", stdin);
- freopen("multimap.out", "w", stdout);
- string com;
- while(cin >> com) {
- if (com == "put") {
- string key, x;
- cin >> key >> x;
- put(key, x);
- }
- if (com == "delete") {
- string key, x;
- cin >> key >> x;
- del(key, x);
- }
- if (com == "deleteall") {
- string key;
- cin >> key;
- del_all(key);
- }
- if (com == "get") {
- string key;
- cin >> key;
- get(key);
- cout << memes.size() << " ";
- for (const auto & i : memes)
- cout << i << " ";
- cout << endl;
- memes.resize(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement