Advertisement
smatskevich

Seminar14

Dec 19th, 2022
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <queue>
  4. #include <string>
  5. #include <unordered_map>
  6. #include <unordered_set>
  7.  
  8. int main1() {
  9.   int n = 0; std::cin >> n;
  10.   std::unordered_set<std::string> accounts;
  11.   std::unordered_set<std::string> spamers;
  12.   for (int i = 0; i < n; ++i) {
  13.     std::string key; std::cin >> key;
  14.     if (!accounts.contains(key)) {
  15.       accounts.insert(key);
  16.     } else {
  17.       spamers.insert(key);
  18.     }
  19.   }
  20.   for (const auto& spamer : spamers) {
  21.     std::cout << spamer << "\n";
  22.   }
  23.   return 0;
  24. }
  25.  
  26. int main2() {
  27.   int n = 0; std::cin >> n;
  28.   // 0 – аккаунт не спамер, 1 – спамер
  29.   std::unordered_map<std::string, bool> accounts;
  30.   for (int i = 0; i < n; ++i) {
  31.     std::string key; std::cin >> key;
  32.     if (!accounts.contains(key)) {
  33.       accounts[key] = false;
  34.     } else {
  35.       accounts[key] = true;
  36.     }
  37.   }
  38. //  for (std::unordered_map<std::string, bool>::iterator it = accounts.begin(); it != accounts.end(); ++it) {
  39. //    if (it->second) std::cout << it->first << "\n";
  40. //  }
  41.   for (auto& [account, is_spamer] : accounts) {
  42.     if (is_spamer) std::cout << account << "\n";
  43.   }
  44.   return 0;
  45. }
  46.  
  47. int main3() {
  48.   int n = 0; std::cin >> n;
  49.   // Значение – число посылок
  50.   std::unordered_map<std::string, int> accounts;
  51.   for (int i = 0; i < n; ++i) {
  52.     std::string key; std::cin >> key;
  53. //    if (!accounts.contains(key)) accounts[key] = 1;
  54. //    else ++accounts[key];
  55.     ++accounts[key];
  56.   }
  57.   for (auto& [account, submits] : accounts) {
  58.     if (submits >= 3) std::cout << account << "\n";
  59.   }
  60.   return 0;
  61. }
  62.  
  63. int main4() {
  64.   int n = 0; std::cin >> n;
  65.  
  66.   std::unordered_map<std::string, std::unordered_set<std::string>> connections;
  67.   for (int i = 0; i < n; ++i) {
  68.     std::string key1, key2, key3; std::cin >> key1 >> key2 >> key3;
  69.     connections[key1].insert(key2);
  70.     connections[key2].insert(key1);
  71.     connections[key1].insert(key3);
  72.     connections[key3].insert(key1);
  73.     connections[key2].insert(key3);
  74.     connections[key3].insert(key2);
  75.   }
  76.  
  77.   // Значение – число Исенбаева
  78.   std::map<std::string, int> accounts;
  79.   for (auto& [key, _] : connections) accounts[key] = -1;
  80.   accounts["Isenbaev"] = 0;
  81.   // Обход BFS
  82.   std::queue<std::string> q;
  83.   q.push("Isenbaev");
  84.   while (!q.empty()) {
  85.     std::string current = q.front(); q.pop();
  86.     for (auto& frnd : connections[current]) {
  87.       if (accounts[frnd] == -1) {
  88.         q.push(frnd);
  89.         accounts[frnd] = accounts[current] + 1;
  90.       }
  91.     }
  92.   }
  93.  
  94.   for (auto& [account, Isenbaev_count] : accounts) {
  95.     std::cout << account << " " << (Isenbaev_count == -1 ? "undefined" : std::to_string(Isenbaev_count)) << "\n";
  96.   }
  97.   return 0;
  98. }
  99.  
  100. struct Account {
  101.   std::string Name;
  102.   int Score = 0;
  103.  
  104.   bool operator==(const Account& other) const { return Name == other.Name && Score == other.Score; }
  105. };
  106.  
  107. namespace std {
  108.   template <>
  109.   struct hash<Account> {
  110.     size_t operator()(const Account& a) const {
  111.       return std::hash<std::string>()(a.Name) * 37 + a.Score;
  112.     }
  113.   };
  114. }
  115.  
  116. struct MyHash2 {
  117.   size_t operator()(const Account& a) const {
  118.     return std::hash<std::string>()(a.Name) * 37 + a.Score;
  119.   }
  120. };
  121.  
  122. int main() {
  123.   std::string s = "Isenbaev";
  124.   std::cout << std::hash<std::string>()(s) << std::endl;
  125.  
  126.   std::unordered_set<Account, MyHash2> ht;
  127.   ht.insert({"Oparin", 1});
  128.   for (auto& key : ht) std::cout << key.Name << " ";
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement