关注
//第二题详解
#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-21 19:15
门头沟学院 Java 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 我的求职总结 #
474093次浏览 6725人参与
# 跟HR说什么能被秒回? #
46167次浏览 329人参与
# 在爱玛,骑向未来 #
48714次浏览 461人参与
# 总结:offer选择,我是怎么选的 #
296173次浏览 1581人参与
# 聊聊这家公司值得去吗 #
962348次浏览 4780人参与
# 你有哪些缓解焦虑的方法? #
62757次浏览 921人参与
# 拼多多工作体验 #
60667次浏览 426人参与
# 得物app工作体验 #
66764次浏览 117人参与
# 你后悔自己读研吗? #
65469次浏览 332人参与
# 小厂实习有必要去吗 #
93527次浏览 440人参与
# 这些公司卡简历很严格 #
106674次浏览 469人参与
# 你找工作的时候用AI吗? #
211699次浏览 1027人参与
# AI让海力士市值突破9000亿美元 #
8210次浏览 99人参与
# 你的秋招第一场笔试是哪家 #
339722次浏览 2203人参与
# 重来一次,我还会选择这个专业吗 #
469848次浏览 4001人参与
# 通信硬件2024笔试面试经验 #
297723次浏览 2095人参与
# 在国企工作的人,躺平了吗? #
427366次浏览 3993人参与
# 携程求职进展汇总 #
964988次浏览 6248人参与
# 毕业季,你想好怎么跟生活对线了吗? #
299506次浏览 3895人参与
# 什么专业适合考公 #
71617次浏览 413人参与
# 研究所笔面经互助 #
127940次浏览 585人参与

查看21道真题和解析