关注
#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
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 找工作,行业重要还是岗位重要? #
6664次浏览 85人参与
# 盲审过后你想做什么? #
12255次浏览 108人参与
# 五一之后,实习真的很难找吗? #
44041次浏览 311人参与
# 领导秒批的请假话术 #
9543次浏览 73人参与
# 安克创新求职进展汇总 #
32469次浏览 413人参与
# 如果不工作真的会快乐吗 #
100839次浏览 862人参与
# 每人推荐一个小而美的高薪公司 #
72816次浏览 1357人参与
# 京东工作体验 #
12945次浏览 90人参与
# 五一假期,你打算“躺”还是“卷”? #
25006次浏览 392人参与
# 考研可以缓解求职焦虑吗 #
20376次浏览 241人参与
# 如何缓解入职前的焦虑 #
171584次浏览 1267人参与
# 面试等了一周没回复,还有戏吗 #
115156次浏览 1072人参与
# 找工作前vs找工作后的心路变化 #
7093次浏览 64人参与
# 应届生薪资多少才合理? #
3034次浏览 24人参与
# 写简历别走弯路 #
714048次浏览 7848人参与
# 你喜欢工作还是上学 #
37268次浏览 407人参与
# 如果有时光机,你最想去到哪个年纪? #
43181次浏览 765人参与
# 牛友们的论文几号送审 #
27123次浏览 623人参与
# 扒一扒那些奇葩实习经历 #
41466次浏览 770人参与
# 24届的你们现状如何了? #
64485次浏览 377人参与