fork download
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <string>
  4.  
  5. class UserRegistry {
  6. public:
  7. void UserEntered(const std::string& user) {
  8. RegisterUserAction(user, "entered");
  9. }
  10.  
  11. void UserExited(const std::string& user) {
  12. RegisterUserAction(user, "exited");
  13. }
  14.  
  15. private:
  16. void LogAction(const std::string& user, const std::string& action, bool is_anomaly = false) {
  17. std::cout << user << " " << action << (is_anomaly ? " (ANOMALY)" : "") << std::endl;
  18. }
  19.  
  20. void RegisterUserAction(const std::string& user, const std::string& action) {
  21. bool anomaly = false;
  22. if (action == "entered" && registry[user] % 2 == 1) {
  23. anomaly = true;
  24. }
  25.  
  26. if (action == "exited" && registry[user] % 2 == 0) {
  27. anomaly = true;
  28. }
  29.  
  30. if (!anomaly)
  31. registry[user]++;
  32.  
  33. LogAction(user, action, anomaly);
  34. }
  35.  
  36. private:
  37. std::unordered_map<std::string, int> registry;
  38. };
  39.  
  40. int main() {
  41. UserRegistry registry;
  42. int log_length;
  43. std::cin >> log_length;
  44. for (int i = 0; i < log_length; ++i) {
  45. std::string user, action;
  46. std::cin >> action >> user;
  47.  
  48. if (action == "entry")
  49. registry.UserEntered(user);
  50. else if (action == "exit")
  51. registry.UserExited(user);
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 16072KB
stdin
8
entry Abbey
entry Abbey
exit Abbey
entry Tyrone
exit Mason
entry Demetra
exit Latonya
entry Idella
stdout
Abbey entered
Abbey entered (ANOMALY)
Abbey exited
Tyrone entered
Mason exited (ANOMALY)
Demetra entered
Latonya exited (ANOMALY)
Idella entered