Advertisement
patrikey

domain_filter.cpp

May 30th, 2023
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | Source Code | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  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(string name) : name_(move(name)) {
  14.         name_.insert(0, 1, '.');
  15.     }
  16.     bool operator==(const Domain& other) const {
  17.         return name_ == other.name_;
  18.     }
  19.     bool operator<(const Domain& other) {
  20.         return lexicographical_compare(name_.begin(), name_.end(), other.name_.begin(), other.name_.end());
  21.     }
  22.     bool IsSubdomain(const Domain& root) const {
  23.         string reverse_domain = string{ name_.rbegin(), name_.rend() };
  24.         string reverse_root = string{ root.name_.rbegin(), root.name_.rend() };
  25.         if (reverse_domain.find(reverse_root) == 0) {
  26.             return true;
  27.         }
  28.         return false;
  29.     }
  30. private:
  31.     string name_;
  32. };
  33.  
  34. template <typename InputIt>
  35. class DomainChecker {
  36. public:
  37.     DomainChecker(InputIt begin, InputIt end) : begin_(begin), end_(end) {}
  38.     bool IsForbidden(const Domain& domain) {
  39.         sort(begin_, end_);
  40.         for (auto it = begin_; it != end_; ++it) {
  41.             if (domain.IsSubdomain(*it) || domain == *it) {
  42.                 return true;
  43.             }
  44.         }
  45.         return false;
  46.     }
  47. private:
  48.     InputIt begin_;
  49.     InputIt end_;
  50. };
  51.  
  52. vector<Domain> ReadDomains(istream& in, int count) {
  53.     vector<Domain> domains;
  54.     for (int i = 0; i < count; ++i) {
  55.         string domain_name;
  56.         getline(in, domain_name);
  57.         domains.push_back(Domain(domain_name));
  58.     }
  59.     return domains;
  60. }
  61.  
  62.  
  63. template <typename Number>
  64. Number ReadNumberOnLine(istream& input) {
  65.     string line;
  66.     getline(input, line);
  67.  
  68.     Number num;
  69.     std::istringstream(line) >> num;
  70.  
  71.     return num;
  72. }
  73.  
  74. void TestIsSubdomain() {
  75.     Domain d{ "abc.com.ru" };
  76.     assert(d.IsSubdomain(Domain{ "ru" }));
  77.     assert(!d.IsSubdomain(Domain{ ".om" }));
  78.     assert(d.IsSubdomain(Domain{ "com.ru" }));
  79.     assert(!d.IsSubdomain(Domain{ "xyz.abc.com.ru" }));
  80.     assert(d.IsSubdomain(Domain{ "abc.com.ru" }));
  81. }
  82.  
  83. int main() {
  84.     TestIsSubdomain();
  85.  
  86.     const std::vector<Domain> forbidden_domains = ReadDomains(cin, ReadNumberOnLine<size_t>(cin));
  87.     DomainChecker checker(forbidden_domains.begin(), forbidden_domains.end());
  88.  
  89.     const std::vector<Domain> test_domains = ReadDomains(cin, ReadNumberOnLine<size_t>(cin));
  90.     for (const Domain& domain : test_domains) {
  91.         cout << (checker.IsForbidden(domain) ? "Bad"sv : "Good"sv) << endl;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement