关注
//第二题详解
#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;
}
查看原帖
点赞 评论
相关推荐
04-01 17:56
南开大学 Web前端 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 我的求职总结 #
474408次浏览 6730人参与
# 跟HR说什么能被秒回? #
46190次浏览 329人参与
# 在爱玛,骑向未来 #
48818次浏览 461人参与
# 总结:offer选择,我是怎么选的 #
296191次浏览 1581人参与
# 你有哪些缓解焦虑的方法? #
62779次浏览 921人参与
# 拼多多工作体验 #
60707次浏览 426人参与
# 得物app工作体验 #
66808次浏览 117人参与
# 聊聊这家公司值得去吗 #
962422次浏览 4781人参与
# 小厂实习有必要去吗 #
93564次浏览 441人参与
# 你后悔自己读研吗? #
65477次浏览 332人参与
# 应届生应该先就业还是先择业 #
200400次浏览 942人参与
# 产品薪资爆料 #
181308次浏览 867人参与
# 这些公司卡简历很严格 #
106703次浏览 469人参与
# AI让海力士市值突破9000亿美元 #
8247次浏览 99人参与
# 你找工作的时候用AI吗? #
211716次浏览 1027人参与
# 你的秋招第一场笔试是哪家 #
339747次浏览 2203人参与
# 重来一次,我还会选择这个专业吗 #
469886次浏览 4001人参与
# 通信硬件2024笔试面试经验 #
297740次浏览 2095人参与
# 在国企工作的人,躺平了吗? #
427384次浏览 3993人参与
# 携程求职进展汇总 #
965007次浏览 6248人参与
# 牛油的搬砖plog #
204358次浏览 1321人参与

查看17道真题和解析