Advertisement
kutuzzzov

Урок 10 Практика - запрещенные домены

Jun 14th, 2023
1,062
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <algorithm>
  2. #include <execution>
  3. #include <iostream>
  4. #include <string>
  5. #include <sstream>
  6. #include <string_view>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. class Domain {
  12. public:
  13.     Domain(const string& str)
  14.         : domain_(str) {
  15.     }
  16.  
  17.     bool operator==(const Domain& rhs) const {
  18.         return (domain_ == rhs.domain_);
  19.     }
  20.    
  21.     bool IsSubDomain(const Domain& rhs) const {
  22.         if (rhs.domain_.size() <= domain_.size()) {
  23.             return equal(rhs.domain_.begin(), rhs.domain_.end(), domain_.begin());
  24.         }
  25.         else {
  26.             return false;
  27.         }
  28.     }
  29.  
  30.     string GetValue() const {
  31.         return domain_;
  32.     }
  33.  
  34. private:
  35.     string domain_;
  36. };
  37.  
  38. class DomainChecker {
  39. public:
  40.     template <typename It>
  41.     DomainChecker(It begin, It end)
  42.         : forbidden_(begin, end) {
  43.         SortUnique();
  44.     }
  45.  
  46.     bool IsForbidden(const Domain& domain) {
  47.         if (forbidden_.empty()) {
  48.             return false;
  49.         }
  50.         auto it = upper_bound(forbidden_.begin(), forbidden_.end(), domain, [](const Domain& value, const Domain& rhs) {
  51.             return value.GetValue() < rhs.GetValue();
  52.             });
  53.         if (it != forbidden_.begin()) {
  54.             return domain.IsSubDomain(forbidden_[it - forbidden_.begin() - 1]);
  55.         }
  56.         else {
  57.             return domain.IsSubDomain(forbidden_[it - forbidden_.begin()]);
  58.         }
  59.         return false;
  60.     }
  61. private:
  62.     void SortUnique() {
  63.         sort(forbidden_.begin(), forbidden_.end(), [](const Domain& lhs, const Domain& rhs) {
  64.             return lhs.GetValue() < rhs.GetValue();
  65.             });
  66.         auto last = unique(forbidden_.begin(), forbidden_.end(), [](const Domain& lhs, const Domain& rhs) {
  67.                 return rhs.IsSubDomain(lhs);
  68.             });
  69.         forbidden_.erase(last, forbidden_.end());
  70.     }
  71.  
  72.     vector<Domain> forbidden_;
  73. };
  74.  
  75. template <typename Number>
  76. vector<Domain> ReadDomains(istream& input, Number number) {
  77.     vector<Domain> result;
  78.     for (Number i = 0; i < number; ++i) {
  79.         string line;
  80.         getline(input, line);
  81.         line.insert(0, 1, '.');
  82.         reverse(line.begin(), line.end());
  83.         result.emplace_back(move(line));
  84.     }
  85.     return result;
  86. }
  87.  
  88. template <typename Number>
  89. Number ReadNumberOnLine(istream& input) {
  90.     string line;
  91.     getline(input, line);
  92.  
  93.     Number num;
  94.     std::istringstream(line) >> num;
  95.  
  96.     return num;
  97. }
  98.  
  99. int main() {
  100.     const std::vector<Domain> forbidden_domains = ReadDomains(cin, ReadNumberOnLine<size_t>(cin));
  101.     DomainChecker checker(forbidden_domains.begin(), forbidden_domains.end());
  102.  
  103.     const std::vector<Domain> test_domains = ReadDomains(cin, ReadNumberOnLine<size_t>(cin));
  104.     for (const Domain& domain : test_domains) {
  105.         cout << (checker.IsForbidden(domain) ? "Bad"sv : "Good"sv) << endl;
  106.     }
  107. }
Advertisement
Comments
  • meraki09
    1 year
    # text 0.10 KB | 0 1
    1. Luxury Homestay in kasauli https://www.merakiholidayhomes.com/ Meraki Holiday Homes is the perfect getaway
Add Comment
Please, Sign In to add comment
Advertisement