Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <sstream>
  4. #include <map>
  5.  
  6. #pragma GCC optimize ("O3")
  7.  
  8. int main()
  9. {
  10.     std::cout.sync_with_stdio(false);
  11.     std::cin.sync_with_stdio(false);
  12.     std::cin.tie(nullptr);
  13.  
  14.     typedef std::pair<int, std::string> decoded;
  15.  
  16.     auto decoded_comparator = [](const decoded &lhs, const decoded &rhs) {
  17.         return lhs.first <= rhs.first;
  18.     };
  19.  
  20.     std::map<std::string, std::string> for_decoding;
  21.     std::priority_queue<decoded, std::vector<decoded>, decltype(decoded_comparator)> messages(decoded_comparator);
  22.     std::string line, from, message, current;
  23.  
  24.     std::getline(std::cin >> std::ws, line);
  25.     while (line != "end")
  26.     {
  27.         if (line == "report")
  28.         {
  29.             if (!messages.empty())
  30.             {
  31.                 std::cout << messages.top().second << std::endl;
  32.                 messages.pop();
  33.             }
  34.             else
  35.             {
  36.                 std::cout << "[no new messages]" << std::endl;
  37.             }
  38.         }
  39.         else
  40.         {
  41.             std::stringstream ss(line);
  42.  
  43.             if (ss && (ss >> std::ws >> from >> std::ws >> message))
  44.             {
  45.                 if (for_decoding.find(from) == for_decoding.end())
  46.                 {
  47.                     for_decoding[from] = message;
  48.                 }
  49.  
  50.                 if (for_decoding[from].length() == message.length())
  51.                 {
  52.                     bool completed = true;
  53.                     current = for_decoding[from];
  54.                     for (int i = 0; i < current.length(); i++)
  55.                     {
  56.                         if (current[i] == '?')
  57.                         {
  58.                             if (message[i] != '?')
  59.                             {
  60.                                 current[i] = message[i];
  61.                             }
  62.                             else
  63.                             {
  64.                                 completed = false;
  65.                             }
  66.                         }
  67.                     }
  68.  
  69.                     if (completed)
  70.                     {
  71.                         std::size_t prio_start = current.find_first_of("0123456789");
  72.                         std::size_t prio_end = current.find_last_of("0123456789");
  73.  
  74.                         if (prio_start != std::string::npos && prio_end != std::string::npos)
  75.                         {
  76.                             int priority = std::stoi(current.substr(prio_start, prio_end + 1));
  77.                             current.erase(prio_start, prio_end - prio_start + 1);
  78.                             messages.push(decoded(priority, current));
  79.                         }
  80.                     }
  81.  
  82.                     for_decoding[from] = current;
  83.                 }
  84.             }
  85.         }
  86.  
  87.         std::getline(std::cin >> std::ws, line);
  88.     }
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement