关注
#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
相关推荐
点赞 评论 收藏
转发
点赞 评论 收藏
转发
投递禾多科技等公司10个岗位 >
点赞 评论 收藏
转发
牛客热帖
正在热议
# 牛友的五一计划 #
28139次浏览 485人参与
# 市场营销面经 #
1876次浏览 82人参与
# 牛客帮帮团来啦!有问必答 #
423984次浏览 8057人参与
# 许愿池 #
67458次浏览 1489人参与
# 晒一晒我的offer #
2873354次浏览 50270人参与
# 2022届毕业生现状 #
287592次浏览 4121人参与
# 你的秋招进展怎么样了 #
450947次浏览 12996人参与
# 如何看待offer收割机的行为 #
199339次浏览 3039人参与
# 互联网公司评价 #
64261次浏览 912人参与
# 硬件人的春招flag #
14806次浏览 203人参与
# 非技术岗薪资爆料 #
11183次浏览 211人参与
# 实习好累,可以辞职全力准备秋招吗 #
2809次浏览 60人参与
# 找工作,你会甘心进小厂还是猛冲大厂 #
26023次浏览 262人参与
# 产品实习,你更倾向大公司or小公司 #
31748次浏览 499人参与
# 秋招开了,你想投哪些公司呢 #
102794次浏览 3135人参与
# 浅聊一下我实习的辛苦费 #
71860次浏览 656人参与
# 提前批真的不会影响正式批吗 #
18389次浏览 231人参与
# 双非本科求职如何逆袭 #
176103次浏览 2643人参与
# 在国企工作的人,躺平了吗? #
74852次浏览 917人参与
# 实习想申请秋招offer,能不能argue薪资 #
4638次浏览 70人参与