关注
#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
相关推荐
05-30 15:13
安徽财经大学 C++ 点赞 评论 收藏
分享
wu970:来个仓库链接,开战开战
点赞 评论 收藏
分享
牛客热帖
更多
- 1... 简历上写了AI项目,怎么才能拉开差距?8426
- 2... 我用一场重病,换来了与大厂的和解7691
- 3... 不会AI Coding真的不行吗?5532
- 4... 字节跳动-中国交易与广告面经3373
- 5... 战报分享——2026年前端社招找工作感想1657
- 6... 字节跳动-Agent开发实习生 一面(45分钟):1434
- 7... 实习还有试用期???1404
- 8... 哈哈哈哈哈哈哈哈哈再也不用写代码啦!!1344
- 9... 学AI+测试力竭的感觉谁懂,WDF越学感觉自己越菜!1313
- 10... Claude Code 压缩机制曝光,这波真的有点猛啊!1175
正在热议
更多
# 我的实习日记 #
4173815次浏览 33325人参与
# 城市生存手册 #
9133次浏览 128人参与
# 第3届现代汽车Code Faster急速编程挑战赛 #
36208次浏览 542人参与
# 牛客AI文生图 #
26635次浏览 271人参与
# 你的房租占工资的比例是多少? #
105109次浏览 915人参与
# 如果人间有后悔药 #
13320次浏览 279人参与
# 入职第五天,你被拉进了几个工作群 #
33551次浏览 85人参与
# 柠檬微趣笔试 #
5716次浏览 68人参与
# 入职第一天,你准备什么时候下班 #
129675次浏览 531人参与
# 实习最晚的一次下班是几点 #
40657次浏览 178人参与
# 哪些公司对双非友好 #
245266次浏览 1293人参与
# 双非本科的出路是什么? #
238442次浏览 1671人参与
# 你现在的工作,是“成长”还是“消耗”? #
33863次浏览 259人参与
# 听到哪句话就代表面试稳了or挂了? #
280034次浏览 1801人参与
# 秋招感动瞬间 #
128947次浏览 572人参与
# 用一句话形容你的团队氛围 #
54078次浏览 316人参与
# 为了找工作你投递了多少公司? #
128973次浏览 802人参与
# 新凯来求职进展汇总 #
85407次浏览 197人参与
# 现在入门AI应该走哪些方向? #
36291次浏览 205人参与
# 提前批过来人的忠告 #
177496次浏览 1344人参与
# 秋招投递记录 #
433318次浏览 3332人参与

