Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <string>
- #include <set>
- #include <stack>
- #include <queue>
- #include <deque>
- using namespace std;
- #define TASK "multimap"
- vector < pair < string, vector < string > > > hash_map[1010101];
- int hash_f(string s) {
- int p = 359;
- int hs = 0;
- for (int i = 0; i < s.size(); i++) {
- hs = (hs * p + s[i]) % 1000000;
- }
- return hs;
- }
- void get(string key) {
- int hs = hash_f(key), sz = 0;
- for (int i = 0; i < hash_map[hs].size(); i++) {
- if (hash_map[hs][i].first == key) {
- for (int j = 0; j < hash_map[hs][i].second.size(); j++) {
- if (hash_map[hs][i].second[j] != "") {
- sz++;
- }
- }
- }
- }
- for (int i = 0; i < hash_map[hs].size(); i++) {
- if (hash_map[hs][i].first == key) {
- cout << sz << " ";
- for (int j = 0; j < hash_map[hs][i].second.size(); j++) {
- if (hash_map[hs][i].second[j] != "") {
- cout << hash_map[hs][i].second[j] << " ";
- }
- }
- cout << "\n";
- return;
- }
- }
- }
- void put(string key, string s) {
- int hs = hash_f(key), t = -1;
- for (int i = 0; i < hash_map[hs].size(); i++) {
- if (hash_map[hs][i].first == key) {
- for (int j = 0; j < hash_map[hs][i].second.size(); j++) {
- if (hash_map[hs][i].second[j] == s) {
- return;
- }
- if (hash_map[hs][i].second[j] == "" && t == -1) {
- t = j;
- }
- }
- if (t != -1) {
- hash_map[hs][i].second[t] = s;
- }
- else {
- hash_map[hs][i].second.push_back(s);
- }
- return;
- }
- }
- vector < string > a;
- a.push_back(s);
- hash_map[hs].push_back(make_pair(key, a));
- }
- void del(string key, string s) {
- int hs = hash_f(key), t = -1;
- for (int i = 0; i < hash_map[hs].size(); i++) {
- if (hash_map[hs][i].first == key) {
- for (int j = 0; j < hash_map[hs][i].second.size(); j++) {
- if (hash_map[hs][i].second[j] == s) {
- hash_map[hs][i].second[j] = "";
- return;
- }
- }
- }
- }
- }
- void delall(string key) {
- int hs = hash_f(key);
- for (int i = 0; i < hash_map[hs].size(); i++) {
- if (hash_map[hs][i].first == key) {
- hash_map[hs][i].second = { };
- }
- }
- }
- /*string next_string(string s) {
- for (int i = s.size() - 1; i >= 0; i--) {
- if (s[i] == 'z') {
- s[i] = 'a';
- continue;
- }
- s[i] = char(s[i] + 1);
- return s;
- }
- for (int i = 0; i < s.size(); i++) {
- s[i] = 'a';
- }
- return s;
- }*/
- int main() {
- #ifdef _DEBUG
- freopen("debug.in", "r", stdin);
- freopen("debug.out", "w", stdout);
- #else
- freopen(TASK".in", "r", stdin);
- freopen(TASK".out", "w", stdout);
- #endif // _DEBUG
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- string a, key, s;
- while (cin >> a) {
- cin >> key;
- if (a[0] == 'p') {
- cin >> s;
- put(key, s);
- }
- if (a[0] == 'g') {
- get(key);
- }
- if (a[0] == 'd') {
- if (a.size() == 6) {
- cin >> s;
- del(key, s);
- }
- else {
- delall(key);
- }
- }
- }
- /*string s = "", c;
- for (int i = 1; i <= 5; i++) {
- s += 'a';
- c = s;
- do {
- if (hash_f(c) == 97) {
- cout << c << "\n";
- }
- c = next_string(c);
- } while (s != c);
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement