Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <list>
- #include <unordered_set>
- #include <random>
- #include <ctime>
- using namespace std;
- #define ll unsigned long long
- ll p = 1111111111111111111ll;
- uint32_t sz = 100;
- vector<list<pair<string, unordered_set<string>>>> vec
- = vector<list<pair<string, unordered_set<string>>>>(sz);
- ll numberOfElements = 0;
- ll myRand() {
- mt19937 gen(static_cast<unsigned int>(time(nullptr)));
- return (ll) uniform_int_distribution<unsigned int>(1, static_cast<unsigned int>(1e9))(gen);
- }
- ll a = myRand();
- uint32_t myHashX(string x) {
- ll hash = 0;
- for (char i : x) {
- hash *= a;
- hash += i;
- hash %= p;
- }
- hash %= sz;
- return (uint32_t) hash;
- }
- void rehash() {
- sz *= 2;
- vector<list<pair<string, unordered_set<string>>>> vec2;
- vec2.resize(sz);
- for (auto i : vec) {
- for (auto k : i) {
- uint32_t hashX = myHashX(k.first);
- pair<string, unordered_set<string>> p;
- p.first = k.first;
- for (const auto &j : k.second) {
- p.second.insert(j);
- }
- vec2[hashX].push_front(p);
- }
- }
- vec = vec2;
- }
- vector<string> ans = vector<string>(500);
- int get(const string &x, uint32_t hashX) {
- uint32_t cnt = 0;
- for (auto &it : vec[hashX]) {
- if (it.first == x) {
- for (const auto &t : it.second) {
- ans[cnt++] = t;
- }
- }
- }
- return cnt;
- }
- void insert(const string &x, uint32_t hashX, const string &y) {
- for (auto &it : vec[hashX]) {
- if (it.first == x) {
- it.second.insert(y);
- return;
- }
- }
- pair<string, unordered_set<string>> p;
- p.first = x;
- p.second.insert(y);
- vec[hashX].push_front(p);
- numberOfElements++;
- if (numberOfElements > sz) {
- rehash();
- }
- }
- void erase(const string &x, uint32_t hashX, const string &y) {
- for (auto &it : vec[hashX]) {
- if (it.first == x) {
- for (const auto &t : it.second) {
- if (t == y) {
- it.second.erase(t);
- return;
- }
- }
- }
- }
- }
- void deleteAll(const string &x, uint32_t hashX) {
- for (auto it = vec[hashX].begin(); it != vec[hashX].end(); it++) {
- if ((*it).first == x) {
- vec[hashX].erase(it);
- numberOfElements--;
- return;
- }
- }
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- freopen("multimap.in", "r", stdin);
- freopen("multimap.out", "w", stdout);
- string s;
- string x, y;
- while (cin >> s) {
- cin >> x;
- uint32_t hashX = myHashX(x);
- if (s[0] == 'p') {
- cin >> y;
- insert(x, hashX, y);
- } else if (s[0] == 'g') {
- auto t = get(x, hashX);
- cout << t << " ";
- for (uint32_t i = 0; i < (uint32_t) t; i++) {
- cout << ans[i] << " ";
- }
- cout << '\n';
- } else if (s == "delete") {
- cin >> y;
- erase(x, hashX, y);
- } else {
- deleteAll(x, hashX);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement