Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cassert>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <string_view>
- #include <vector>
- using namespace std;
- class Domain {
- public:
- Domain(string name) : name_(move(name)) {
- name_.insert(0, 1, '.');
- }
- bool operator==(const Domain& other) const {
- return name_ == other.name_;
- }
- bool operator<(const Domain& other) {
- return lexicographical_compare(name_.begin(), name_.end(), other.name_.begin(), other.name_.end());
- }
- bool IsSubdomain(const Domain& root) const {
- string reverse_domain = string{ name_.rbegin(), name_.rend() };
- string reverse_root = string{ root.name_.rbegin(), root.name_.rend() };
- if (reverse_domain.find(reverse_root) == 0) {
- return true;
- }
- return false;
- }
- private:
- string name_;
- };
- template <typename InputIt>
- class DomainChecker {
- public:
- DomainChecker(InputIt begin, InputIt end) : begin_(begin), end_(end) {}
- bool IsForbidden(const Domain& domain) {
- sort(begin_, end_);
- for (auto it = begin_; it != end_; ++it) {
- if (domain.IsSubdomain(*it) || domain == *it) {
- return true;
- }
- }
- return false;
- }
- private:
- InputIt begin_;
- InputIt end_;
- };
- vector<Domain> ReadDomains(istream& in, int count) {
- vector<Domain> domains;
- for (int i = 0; i < count; ++i) {
- string domain_name;
- getline(in, domain_name);
- domains.push_back(Domain(domain_name));
- }
- return domains;
- }
- template <typename Number>
- Number ReadNumberOnLine(istream& input) {
- string line;
- getline(input, line);
- Number num;
- std::istringstream(line) >> num;
- return num;
- }
- void TestIsSubdomain() {
- Domain d{ "abc.com.ru" };
- assert(d.IsSubdomain(Domain{ "ru" }));
- assert(!d.IsSubdomain(Domain{ ".om" }));
- assert(d.IsSubdomain(Domain{ "com.ru" }));
- assert(!d.IsSubdomain(Domain{ "xyz.abc.com.ru" }));
- assert(d.IsSubdomain(Domain{ "abc.com.ru" }));
- }
- int main() {
- TestIsSubdomain();
- const std::vector<Domain> forbidden_domains = ReadDomains(cin, ReadNumberOnLine<size_t>(cin));
- DomainChecker checker(forbidden_domains.begin(), forbidden_domains.end());
- const std::vector<Domain> test_domains = ReadDomains(cin, ReadNumberOnLine<size_t>(cin));
- for (const Domain& domain : test_domains) {
- cout << (checker.IsForbidden(domain) ? "Bad"sv : "Good"sv) << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement