shushus

Спринт 13. Практика - запрещенные домены.

Apr 20th, 2023
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4.  
  5. class Domain {
  6. public:
  7.    
  8.     Domain(const std::string& domain) {
  9.        
  10.         std::string add_dot = "." + domain;
  11.         std::reverse(add_dot.begin(), add_dot.end());
  12.        
  13.         domain_title_ = add_dot;
  14.     }
  15.  
  16.     bool operator==(const Domain& other_domain) const {
  17.        
  18.         return std::lexicographical_compare(domain_title_.begin(),
  19.                                             domain_title_.end(),
  20.                                             other_domain.domain_title_.begin(),
  21.                                             other_domain.domain_title_.end());
  22.     }
  23.  
  24.     bool IsSubdomain(const Domain& other_domain) const {
  25.        
  26.         if (!(other_domain.domain_title_.size() > domain_title_.size())) {
  27.            
  28.             if (std::equal(other_domain.domain_title_.begin(),
  29.                            other_domain.domain_title_.end(),
  30.                            domain_title_.begin())) {
  31.                 return true;
  32.                
  33.             } else {
  34.                 return false;
  35.             }
  36.            
  37.         } else {
  38.             return false;
  39.         }
  40.     }
  41.  
  42.     std::string GetDomain() const {return domain_title_;}
  43.  
  44. private:
  45.     std::string domain_title_;
  46. };
  47.  
  48. class DomainChecker {
  49. public:
  50.  
  51.     template<typename Iterator>
  52.     DomainChecker(const Iterator start, const Iterator end) {
  53.  
  54.         std::vector<Domain> domains(start, end);
  55.        
  56.         std::sort(domains.begin(), domains.end(),
  57.                [](const Domain& lhs, const Domain& rhs) {return lhs == rhs;});
  58.  
  59.         Iterator unique_domains_it = std::unique(domains.begin(), domains.end(),
  60.                                               [](const Domain& lhs, const Domain& rhs) {return rhs.IsSubdomain(lhs);});
  61.        
  62.         domains.erase(unique_domains_it, domains.end());        
  63.         forbidden_ = domains;
  64.     }
  65.  
  66.     bool IsForbidden(const Domain& domain_to_check) {
  67.        
  68.         if (forbidden_.size()) {          
  69.             std::vector<Domain>::iterator domain_pos_it = std::upper_bound(forbidden_.begin(),
  70.                                                                            forbidden_.end(),
  71.                                                                            domain_to_check,
  72.                                                                         [](const Domain& lhs, const Domain& rhs) {
  73.                                                                              return lhs.GetDomain() < rhs.GetDomain();
  74.                                                                         });
  75.  
  76.             if (forbidden_.begin() == domain_pos_it) {
  77.                 return domain_to_check.IsSubdomain(forbidden_[domain_pos_it - forbidden_.begin()]);
  78.  
  79.             } else {
  80.                 return domain_to_check.IsSubdomain(forbidden_[domain_pos_it - forbidden_.begin() - 1]);
  81.                
  82.             }
  83.            
  84.         } else {
  85.            return false;
  86.         }
  87.     }
  88.  
  89. private:
  90.     std::vector<Domain> forbidden_;
  91. };
  92.  
  93. const std::vector<Domain> ReadDomains(std::istream& input, size_t amount) {
  94.    
  95.     std::vector<Domain> domains;
  96.     std::string line;
  97.    
  98.     for (size_t i = 0; i < amount; ++i) {
  99.        
  100.         getline(input, line);
  101.         domains.push_back(line);
  102.     }
  103.    
  104.     return domains;
  105. }
  106.  
  107. template<typename Number>
  108. Number ReadNumberOnLine(std::istream &input) {
  109.     std::string line;
  110.     getline(input, line);
  111.  
  112.     Number num;
  113.     std::istringstream(line) >> num;
  114.  
  115.     return num;
  116. }
  117.  
  118. int main() {
  119.     using namespace std::literals;
  120.    
  121.     const std::vector<Domain> forbidden_domains = ReadDomains(std::cin,
  122.                                                               ReadNumberOnLine<size_t>(std::cin));
  123.                                                              
  124.     DomainChecker checker(forbidden_domains.begin(),
  125.                           forbidden_domains.end());
  126.  
  127.     const std::vector<Domain> test_domains = ReadDomains(std::cin,
  128.                                                          ReadNumberOnLine<size_t>(std::cin));
  129.    
  130.     for (const Domain& domain : test_domains) {
  131.         std::cout << (checker.IsForbidden(domain) ? "Bad"sv : "Good"sv) << std::endl;
  132.        
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment