fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <iomanip>
  5. #include <stack>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <queue>
  10. #include <map>
  11.  
  12. using namespace std;
  13.  
  14. struct status { //status - password and online
  15. string password;
  16. bool online = false;
  17. };
  18.  
  19. map <string, status> database; //user database
  20.  
  21.  
  22. int main () {
  23. int n; cin >> n; //number of queries
  24. typedef std::map<std::string, status> db; // use this typedef for using an iterator
  25. while (n--){
  26. string com, login; //command and login
  27. status query; //for status input
  28. cin >> com;
  29. if (com == "register"){
  30. cin >> login >> query.password;
  31. db::iterator it = database.find(login); //returning an iterator
  32. if (it != database.end()) { //each el from database is in [begin, end). it = database.end() means that there is no such element in db
  33. cout << "fail: user already exists\n";
  34. }
  35. else {
  36. database[login].password = query.password; // register
  37. cout << "success: new user added\n";
  38. }
  39. }
  40. //user can log in if he isn't online but he is registered
  41. if (com == "login") {
  42. cin >> login >> query.password;
  43. db::iterator it = database.find(login);
  44. if (it != database.end()){
  45. if (query.password == database[login].password){
  46. if(!database[login].online){
  47. database[login].online = true;
  48. cout << "success: user logged in\n";
  49. }
  50. else cout << "fail: already logged in\n";
  51. }
  52. else cout << "fail: incorrect password\n";
  53. }
  54. else cout << "fail: no such user\n";
  55. }
  56. //logout
  57. if (com == "logout") {
  58. cin >> login;
  59. if (database.find(login) == database.end()) cout << "fail: no such user\n";
  60. else {
  61. if (!database[login].online) cout << "fail: already logged out\n";
  62. else {
  63.  
  64. database[login].online = false;
  65. cout << "success: user logged out\n";
  66. }
  67. }
  68. }
  69. }
  70. return 0;
  71. }
  72.  
Time limit exceeded #stdin #stdout 5s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty