关注
//第二题详解
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct IPallow{
int flag; //1 allow or 0 deny;
vector<int> ip;
int mask;
};
vector<int> IpStrToIpInt(string ipstr)
{
vector<int> ipint;
int index=0;
while(index!=string::npos)
{
index=ipstr.find('.');
if(index==string::npos) {ipint.push_back(atoi(ipstr.c_str())); break;}
ipint.push_back(atoi((ipstr.substr(0,index)).c_str()));
ipstr=ipstr.substr(index+1,ipstr.length()-index-1);
}
return ipint;
}
vector<IPallow> StringtoIPallow(vector<string> list)
{
vector<IPallow> iplist;
string temp,allow,ipstr,maskstr,right;
IPallow ip;
int space,slash;
for(int i=0;i<list.size();i++)
{
temp=list[i];
space=temp.find(' ');
allow=temp.substr(0,space);
right=temp.substr(space+1,temp.length()-space);
slash=right.find('/');
if(slash!=string::npos)
{
ipstr=right.substr(0,slash);
maskstr=right.substr(slash+1,right.length()-slash);
}
else
{
ipstr=right;
maskstr="32";
}
if(allow=="allow") ip.flag=1;
else ip.flag=0;
ip.mask=atoi(maskstr.c_str());
ip.ip=IpStrToIpInt(ipstr);
iplist.push_back(ip);
}
return iplist;
}
void JudgeIP(vector<string> list,string q)
{
vector<int> qin=IpStrToIpInt(q);
vector<IPallow> iplist=StringtoIPallow(list);
vector<int> temp;
IPallow tempip;
int mask;
for(int i=0;i<iplist.size();i++)
{
temp=qin;
tempip=iplist[i];
mask=32-tempip.mask;
if(mask<=8)
{
tempip.ip[3]=tempip.ip[3]>>mask;
temp[3]=temp[3]>>mask;
if(tempip.ip==temp) {
if(tempip.flag)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return;
}
continue;
}
if(mask<=16)
{
tempip.ip[3]=0; temp[3]=0;
tempip.ip[2]=tempip.ip[2]>>mask-8;
temp[2]=temp[2]>>mask-8;
if(tempip.ip==temp) {
if(tempip.flag)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return;
}
continue;
}
if(mask<=24)
{
tempip.ip[3]=0; temp[3]=0;
tempip.ip[2]=0; temp[2]=0;
tempip.ip[1]=tempip.ip[1]>>mask-16;
temp[1]=temp[1]>>mask-16;
if(tempip.ip==temp) {
if(tempip.flag)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return;
}
continue;
}
if(mask<=32)
{
tempip.ip[3]=0; temp[3]=0;
tempip.ip[2]=0; temp[2]=0;
tempip.ip[1]=0; temp[1]=0;
tempip.ip[0]=tempip.ip[0]>>mask-24;
temp[0]=temp[0]>>mask-24;
if(tempip.ip==temp) {
if(tempip.flag)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return;
}
continue;
}
}
cout<<"No"<<endl;
return;
}
int main(){
int n,m;
cin>>n>>m;
vector<string> list,qu;
string temp;
getchar();
for(int i=0;i<n;i++){ getline(cin,temp); list.push_back(temp);}
for(int i=0;i<m;i++){ getline(cin,temp); qu.push_back(temp);}
for(int i=0;i<m;i++){ JudgeIP(list,qu[i]);}
system("pause");
return 0;
}
查看原帖
点赞 评论
相关推荐
05-25 11:01
东南大学 Java
等闲king:你这个学历慢慢找就好了,现在重心都在暑期上,日常要看运气,多提升一下自己的技术要不简历上只有学历,和同学历竞争还是有劣势 点赞 评论 收藏
分享
牛客热帖
更多
- 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人参与
查看10道真题和解析