关注
//第二题详解
#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;
}
查看原帖
点赞 评论
相关推荐
查看9道真题和解析 点赞 评论 收藏
分享
点赞 评论 收藏
分享
拉鲁拉丝:今天还是考研出分


点赞 评论 收藏
分享
牛客热帖
更多
- 1... 32分钟投了18家网申,效率如何?1.5W
- 2... 急哭了!985科班本三段大厂实习被阿里一脚踹飞!简历都没收!8837
- 3... 秋招以来的AI面经问题6600
- 4... 面试官视角聊聊:如何在AI浪潮中找到好工作?5136
- 5... C++ MySql 常考面试题总结2645
- 6... 面试被问到“agent的记忆机制怎么设计”,该怎么回答?2425
- 7... 美团测开->腾讯后端,感谢那个不愿放弃的自我2359
- 8... 2027届暑期实习大科普,为什么从来没人给你讲过这些事情?1884
- 9... 没算力,能搞大模型吗?1819
- 10... 拒绝无效的努力1804
正在热议
更多
# xx岗简历求拷打 #
17649次浏览 159人参与
# 牛友的志愿填报指南 #
50000次浏览 215人参与
# 开工第一帖 #
59312次浏览 1021人参与
# 找工作有哪些冷知识 #
226689次浏览 2686人参与
# 有转正机会的小厂实习值得去吗? #
12002次浏览 115人参与
# 应届生,你找到工作了吗 #
108764次浏览 650人参与
# 今年形式下双非本找得到工作吗 #
287981次浏览 1639人参与
# 掌握什么AI技能,会为你的求职大大加分 #
13420次浏览 501人参与
# 打工人的精神状态 #
137954次浏览 1506人参与
# 携程求职进展汇总 #
894419次浏览 5953人参与
# 海康威视求职进展汇总 #
570415次浏览 3725人参与
# 如何看待offer收割机的行为 #
1050575次浏览 6614人参与
# 你怎么看待AI面试 #
151756次浏览 805人参与
# 工作不开心辞职是唯一出路吗 #
10811次浏览 44人参与
# 你最讨厌面试被问什么 #
13068次浏览 138人参与
# 面试反问你会问什么 #
170279次浏览 1751人参与
# 你找工作的时候用AI吗? #
177208次浏览 904人参与
# 金三银四,你有感觉到吗 #
698794次浏览 6100人参与
# 选实习,你更看重哪方面? #
68368次浏览 491人参与
# 听劝,这个简历怎么改 #
383239次浏览 1831人参与
