Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_set>
  3. #include <string>
  4. #include <ctime>
  5. #include <cctype>
  6. #include <random>
  7. #include <cstdlib>
  8.  
  9. using std::string;
  10.  
  11. bool CheckUserName(const std::string& user) {
  12.     for (char c : user)
  13.         if (!isalpha(c) && !isdigit(c) && c != '_')
  14.             return false;
  15.     return true;
  16. }
  17.  
  18. bool GetLine(std::string *line, size_t max_length) {
  19.     line->clear();
  20.     while (std::cin && line->size() <= max_length) {
  21.         int symbol = std::cin.get();
  22.         if (symbol == '\n')
  23.             return true;
  24.         line->push_back(symbol);
  25.     }
  26.     return false;
  27. }
  28.  
  29. std::vector<std::string> GetUsers(size_t max_count) {
  30.     std::vector<std::string> users;
  31.     std::string cur_line;
  32.     while (users.size() < max_count) {
  33.         bool is_valid = GetLine(&cur_line, 15u);
  34.         if (std::cin.eof()) {
  35.             break;
  36.         }
  37.         if (!is_valid) {
  38.             std::cout << "Too long username " << cur_line << "\n";
  39.             exit(0);
  40.         }
  41.         if (CheckUserName(cur_line))
  42.             users.push_back(cur_line);
  43.         else {
  44.             std::cout << "Incorrect username " << cur_line << "\n";
  45.             exit(0);
  46.         }
  47.     }
  48.     return users;
  49. }
  50.  
  51. int main(int argc, char **argv) {
  52.     std::ios_base::sync_with_stdio(false);
  53.     std::mt19937 gen(time(nullptr));
  54.     std::uniform_int_distribution<int> dist(30000, 1000000);
  55.     size_t init_size = dist(gen);
  56.     std::unordered_set<string> users(init_size);
  57.  
  58.     auto users_list = GetUsers(15000u);
  59.     auto start_time = std::clock();
  60.  
  61.     for (const auto& user : users_list) {
  62.         users.insert(user);
  63.     }
  64.  
  65.     auto end_time = std::clock();
  66.     double spent = static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC;
  67.     std::cout << "Spent " << spent << " seconds" << std::endl;
  68.     if (spent > 1.5) {
  69.         std::cout << "Shit happens\n";
  70.         return 1;
  71.     }
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement