#include<algorithm> #include<utility> #include<string> #include<sstream> #include<iostream> #include<vector> #include<map> #include<bitset> std::pair<unsigned, unsigned> getad_ms(std::string str) { using namespace std; replace(str.begin(), str.end(), '.', ' '); replace(str.begin(), str.end(), '/', ' '); istringstream istr(str); pair<unsigned, unsigned> pa; for (int i{},j;i < 4;++i) { istr >> j; pa.first += j*(1 << 8 * (3 - i)); } !istr.eof() ? istr >> pa.second : (pa.second = 32, istr); return pa; } void trim_front(std::string& str, char ch) { size_t sz = str.find_first_not_of(ch, 0); str.erase(0, sz); } class search_tree { public: search_tree():head{ new node{} }/*,refuse_without_allow{false}*/{} void add(unsigned add, unsigned mask, bool acc, int num); bool accept(unsigned int add); ~search_tree(); private: enum class states { mid_state ,allow, deny }; struct node { node *left, *right; states acc; int num; node() :left{ nullptr }, right{ nullptr }, acc{ states::mid_state } {} }; node *head; void del(node *p); }; void search_tree::add(unsigned add, unsigned mask, bool acc,int num) { using namespace std; bitset<32> bit(add); node *pos = head; for (int i{};i < mask;++i) { if (pos->acc != states::mid_state) return; if (bit[31-i]) { if (pos->right == nullptr) pos->right = new node{}; pos = pos->right; } else { if (pos->left == nullptr) pos->left = new node{}; pos = pos->left; } } if (pos->acc != states::mid_state) return; pos->num = num; pos->acc = acc ? states::allow : states::deny; } bool search_tree::accept(unsigned int add) { using namespace std; bitset<32> bit(add); node * pos = head, *minnum{ nullptr }; int num=numeric_limits<int>::max(); for (size_t i = 0;i<32&&pos; i++) { if (pos&&pos->acc != states::mid_state) { if (pos->num < num) { num = pos->num; minnum = pos; } } pos = bit[31-i] ? pos->right : pos->left; } if (minnum == nullptr) return true; return minnum->acc == states::allow ? true : false; } search_tree::~search_tree() { del(head); } void search_tree::del(search_tree::node* p) { if (p->left) del(p->left); if (p->right) del(p->right); delete p; } int main() { using namespace std; for (int i, j;cin >> i >> j;) { string str; search_tree treenet; while (isspace(cin.peek())) { cin.get(); } for (int k{};k < i;++k) { getline(cin, str); trim_front(str, ' '); auto pos = str.find(' '); auto ps = getad_ms(string(str, pos + 1)); bool acc = str[0] == 'a' ? true:false; treenet.add(ps.first, ps.second, acc,k); } for (int k{};k < j;++k) { cin >> str; auto pa = getad_ms(str); cout << (treenet.accept(pa.first)?"YES":"NO") << '\n'; } } }
点赞 1

相关推荐

牛客网
牛客企业服务