关注
#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-19 18:48
长沙学院 Java 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 我的实习日记 #
4138678次浏览 33162人参与
# 秋招投递记录 #
429073次浏览 3306人参与
# 第3届现代汽车Code Faster急速编程挑战赛 #
25560次浏览 462人参与
# 你投递的公司有几家约面了? #
174445次浏览 1043人参与
# 产品实习,你更倾向大公司or小公司 #
234687次浏览 2169人参与
# 你认为小厂实习有用吗? #
152630次浏览 804人参与
# 为了找工作你投递了多少公司? #
120709次浏览 766人参与
# 实习返校后,你的精神状态是__? #
47435次浏览 173人参与
# 重来一次,你会对开始求职的自己说 #
55291次浏览 515人参与
# 你投了多少家公司?进展是___ #
251917次浏览 1464人参与
# 通信/硬件求职避坑tips #
179507次浏览 1176人参与
# 如果有时光机,你最想去到哪个年纪? #
81840次浏览 863人参与
# 你小时候最想从事什么职业 #
170858次浏览 2080人参与
# 我的租房踩坑经历 #
230348次浏览 1305人参与
# 发工资后,你做的第一件事是什么 #
108963次浏览 350人参与
# 求职遇到的搞笑事件 #
209762次浏览 1083人参与
# 体制内上岸心路历程 #
42253次浏览 245人参与
# 实习生的生存小技巧 #
41872次浏览 365人参与
# 双非本科的出路是什么? #
236812次浏览 1668人参与
# 米哈游笔试 #
741633次浏览 1270人参与
# 今年形式下双非本找得到工作吗 #
339466次浏览 1795人参与
