关注
//第二题详解
#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;
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 找工作,行业重要还是岗位重要? #
6542次浏览 84人参与
# 盲审过后你想做什么? #
12233次浏览 108人参与
# 五一之后,实习真的很难找吗? #
43818次浏览 311人参与
# 领导秒批的请假话术 #
9457次浏览 72人参与
# 安克创新求职进展汇总 #
32452次浏览 412人参与
# 如果不工作真的会快乐吗 #
100791次浏览 860人参与
# 每人推荐一个小而美的高薪公司 #
72802次浏览 1357人参与
# 京东工作体验 #
12940次浏览 90人参与
# 五一假期,你打算“躺”还是“卷”? #
24430次浏览 388人参与
# 考研可以缓解求职焦虑吗 #
20308次浏览 241人参与
# 如何缓解入职前的焦虑 #
171520次浏览 1267人参与
# 面试等了一周没回复,还有戏吗 #
115114次浏览 1072人参与
# 找工作前vs找工作后的心路变化 #
7085次浏览 64人参与
# 应届生薪资多少才合理? #
3031次浏览 24人参与
# 写简历别走弯路 #
713992次浏览 7848人参与
# 你喜欢工作还是上学 #
37241次浏览 407人参与
# 如果有时光机,你最想去到哪个年纪? #
43157次浏览 765人参与
# 牛友们的论文几号送审 #
27115次浏览 623人参与
# 扒一扒那些奇葩实习经历 #
41413次浏览 770人参与
# 24届的你们现状如何了? #
64463次浏览 377人参与