关注
//第二题详解
#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;
}
查看原帖
点赞 评论
相关推荐
牛客热帖
更多
- 1... 都在找Agent开发,我整理了80道相关的Agent开发面试题。2.1W
- 2... 被笔试耽误了一天day16(为什么携程第三题始终是0呢5230
- 3... 27后端暑期实习-字节-中国广告与交易(已OC3732
- 4... 美团暑期前端一面面经2153
- 5... 双非后端10天速通字节暑期2018
- 6... #拼多多集团-PDD笔试# PDD 3.29 笔试 A了 3道,第四题不太会,有友友A出来了吗,感觉题目比较复杂。1870
- 7... 京东后端面经1830
- 8... 3.29 pdd笔试1749
- 9... 3.29携程笔试1632
- 10... 27届暑期实习腾讯PCG前端面经1628
正在热议
更多
# 大厂实习和小厂实习最大的区别是什么? #
3827次浏览 24人参与
# 参加完秋招的机械人,还参加春招吗? #
120107次浏览 764人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
19442次浏览 312人参与
# 牛友の3月总结 #
3540次浏览 33人参与
# 拼多多工作体验 #
52850次浏览 344人参与
# 面试被问到不会的问题,你怎么应对? #
1027次浏览 12人参与
# 招商银行数字金融训练营 #
40945次浏览 401人参与
# 这些公司卡简历很严格 #
95369次浏览 418人参与
# 研究所VS国企,该如何选 #
259204次浏览 2013人参与
# 通信硬件知识分享 #
48192次浏览 538人参与
# 实习最想跑路的瞬间 #
131057次浏览 740人参与
# 找AI工作可以去哪些公司? #
18812次浏览 869人参与
# 从事AI岗需要掌握哪些技术栈? #
16114次浏览 976人参与
# 你做过最难的笔试是哪家公司 #
49711次浏览 876人参与
# 机械人怎么评价今年的华为 #
231829次浏览 1538人参与
# 材料人的华为红黑体验 #
41617次浏览 200人参与
# 金三银四,你的春招进行到哪个阶段了? #
25130次浏览 300人参与
# 说说你知道的学历厂 #
391088次浏览 1379人参与
# AI面会问哪些问题? #
38128次浏览 1195人参与
# 想给25届机械人的秋招建议 #
47830次浏览 251人参与
# 如何排解工作中的焦虑 #
292850次浏览 2606人参与
# 机械人避雷的岗位/公司 #
62953次浏览 395人参与
