关注
//第二题详解
#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;
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
活泼的代码渣渣在泡池...:同学你好,我也是学院本,后天要面这个亚信科技,是实习,请问问题都啥样呀,我项目就做了网上的,这是第一次面试

点赞 评论 收藏
分享
贴心的火龙果风度翩翩:公司名字很符合 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 第一次找实习,我建议__ #
21923次浏览 292人参与
# 韶音科技求职进展汇总 #
60570次浏览 505人参与
# 从mentor身上学到了__ #
19256次浏览 303人参与
# 你怎么评价今年的春招? #
142342次浏览 1388人参与
# 什么样的公司千万别去 #
16847次浏览 115人参与
# 上班摸鱼,你都在干些什么? #
31375次浏览 227人参与
# 外出实习被同学举报 #
4147次浏览 32人参与
# 你投递的公司有几家约面了? #
149778次浏览 982人参与
# 秋招的嫡长offer #
312683次浏览 1881人参与
# 秋招暂停,我将对以下公司做出处罚__ #
29768次浏览 137人参与
# 秋招结束之后的日子 #
106200次浏览 1017人参与
# 你认为工作的意义是什么 #
203520次浏览 1289人参与
# 秋招我要惩罚这些公司 #
3100次浏览 22人参与
# 打工人的至爽时刻or至暗时刻 #
42006次浏览 221人参与
# 你听到的“最没用”的秋招建议 #
20984次浏览 234人参与
# 如果今天是你的last day,你会怎么度过? #
48202次浏览 299人参与
# 面试被问期望薪资时该如何回答 #
311834次浏览 1789人参与
# 2025秋招体验点评 #
47651次浏览 482人参与
# 除了主业以外,你还有哪些其他收入? #
35308次浏览 302人参与
# 在国企工作的人,躺平了吗? #
375558次浏览 3930人参与
查看14道真题和解析