-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulti-map.cpp
More file actions
130 lines (121 loc) · 3.2 KB
/
multi-map.cpp
File metadata and controls
130 lines (121 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <vector>
using namespace std;
vector<vector<vector<vector<string>>>> multiMap;
vector<vector<string>> Map;
static const int size_1 = 1e2 + 121;
static const int size_2 = 321;
static const int p = 31;
int hash_(string &s) {
int ans = 0;
for (char c : s) {
int x = (int) abs(c - 'a' + 1);
ans = (ans * p + x) % size_1;
}
return ans;
}
void put(string &key, string &value) {
int i = hash_(key);
for (int j = 0; j < size_2; j++) {
if (Map[i][j] == key) {
int k = hash_(value);
for (string &q : multiMap[i][j][k]) {
if (q == value) {
return;
}
}
multiMap[i][j][k].push_back(value);
return;
}
else if (Map[i][j].empty()) {
Map[i][j] = key;
multiMap[i][j].resize(size_1);
int k = hash_(value);
multiMap[i][j][k].push_back(value);
return;
}
}
}
void delete_(string &key, string &value) {
int i = hash_(key);
for (int j = 0; j < size_2; j++) {
if (Map[i][j] == key) {
int k = hash_(value);
for (int q = 0; q < multiMap[i][j][k].size(); q++) {
if (multiMap[i][j][k][q] == value) {
vector<string> tmp;
for (string &z : multiMap[i][j][k]) {
if (z != value) {
tmp.push_back(z);
}
}
multiMap[i][j][k] = tmp;
return;
}
}
}
}
}
void deleteAll(string &key) {
int i = hash_(key);
for (int j = 0; j < size_2; j++) {
if (Map[i][j] == key) {
Map[i][j] = "";
multiMap[i][j].clear();
return;
}
}
}
vector<string> get(string &key) {
vector<string> ans;
int i = hash_(key);
for (int j = 0; j < size_2; j++) {
if (Map[i][j] == key) {
for (int k = 0; k < size_1; k++) {
for (string &q : multiMap[i][j][k]) {
ans.push_back(q);
}
}
return ans;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
freopen("multimap.in", "r", stdin);
freopen("multimap.out", "w", stdout);
multiMap.resize(size_1);
Map.resize(size_1, vector<string>(size_2));
for (int i = 0; i < size_1; i++) {
multiMap[i].resize(size_2);
}
string s;
while (cin >> s) {
string key;
cin >> key;
if (s[0] == 'p') {
string value;
cin >> value;
put(key, value);
}
if (s[0] == 'd' && s.back() == 'e') {
string value;
cin >> value;
delete_(key, value);
}
if (s[0] == 'd' && s.back() == 'l') {
deleteAll(key);
}
if (s[0] == 'g') {
vector<string> ans = get(key);
cout << ans.size() << " ";
for (string &i : ans) {
cout << i << " ";
}
cout << '\n';
}
}
}