关注
//第二题详解
#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;
}
查看原帖
点赞 评论
相关推荐
黑曼巴在线招人:我有一、二、三、四、五与六点羡慕!


点赞 评论 收藏
分享
10-14 21:06
西北农林科技大学 数据分析师 点赞 评论 收藏
分享
牛客热帖
更多
- 1... 面试最后的反问环节,能问些什么?(附特供问题)2.2W
- 2... BG一般,如何逆天改命拿下后端秋招SSP?1.3W
- 3... 从面试官的角度看待一场面试是怎么样的?7303
- 4... 害,找工作哪有不上当的!5286
- 5... 团、节、东孝子全部启动启动启动!(26届后端秋招总结)5236
- 6... 作为普通家庭出身的我,为什么非大厂不可?4556
- 7... 双非硕的十月份秋招总结4102
- 8... 感觉每个人都有自己的苦恼3686
- 9... 项目经历混乱?STAR法则手把手教你梳理(附真实案例分析过程)3651
- 10... 待了一年,一点没亏3245
正在热议
更多
# 实习在多还是在精 #
23860次浏览 189人参与
# 你的房租占工资的比例是多少? #
61144次浏览 742人参与
# 未岚大陆求职进展汇总 #
3026次浏览 48人参与
# 秋招踩过的“雷”,希望你别再踩 #
57017次浏览 829人参与
# 我的求职进度条 #
37022次浏览 577人参与
# 大厂VS公务员你怎么选 #
13335次浏览 216人参与
# 智慧芽求职进展汇总 #
486次浏览 5人参与
# 如果不考虑收入,你最想做什么工作? #
30942次浏览 180人参与
# 柠檬微趣工作体验 #
13091次浏览 72人参与
# 机械人的保底公司是哪一家? #
40490次浏览 133人参与
# 顺丰求职进展汇总 #
61836次浏览 306人参与
# 华为池子有多大 #
102018次浏览 732人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
135293次浏览 868人参与
# 如果再来一次,你还会学硬件吗 #
137719次浏览 1441人参与
# 如何用一句话描述你的职业 #
24788次浏览 172人参与
# 高学历就一定能找到好工作吗? #
55382次浏览 607人参与
# 如何排解工作中的焦虑 #
219457次浏览 2104人参与
# 实习下班不想学习,正常吗? #
13546次浏览 150人参与
# 反问环节如何提问 #
112237次浏览 2344人参与
# 你见过哪些工贼行为 #
11265次浏览 76人参与
# 工作中,努力重要还是选择重要? #
204306次浏览 2074人参与
# 校招谈薪一定要知道的事 #
9427次浏览 92人参与