描述
Little Hi runs a web server. Sometimes he has to deny access from a certain set of malicious IP addresses while his friends are still allow to access his server. To do this he writes N rules in the configuration file which look like:
allow 1.2.3.4/30 deny 1.1.1.1 allow 127.0.0.1 allow 123.234.12.23/3 deny 0.0.0.0/0
Each rule is in the form: allow | deny address or allow | deny address/mask.
When there comes a request, the rules are checked in sequence until the first match is found. If no rule is matched the request will be allowed. Rule and request are matched if the request address is the same as the rule address or they share the same first mask digits when both written as 32bit binary number.
For example IP "1.2.3.4" matches rule "allow 1.2.3.4" because the addresses are the same. And IP "128.127.8.125" matches rule "deny 128.127.4.100/20" because 10000000011111110000010001100100 (128.127.4.100 as binary number) shares the first 20 (mask) digits with10000000011111110000100001111101 (128.127.8.125 as binary number).
Now comes M access requests. Given their IP addresses, your task is to find out which ones are allowed and which ones are denied.
输入
Line 1: two integers N and M.
Line 2-N+1: one rule on each line.
Line N+2-N+M+1: one IP address on each line.
All addresses are IPv4 addresses(0.0.0.0 - 255.255.255.255). 0 <= mask <= 32.
For 40% of the data: 1 <= N, M <= 1000.
For 100% of the data: 1 <= N, M <= 100000.
输出
For each request output "YES" or "NO" according to whether it is allowed.
5 5 allow 1.2.3.4/30 deny 1.1.1.1 allow 127.0.0.1 allow 123.234.12.23/3 deny 0.0.0.0/0 1.2.3.4 1.2.3.5 1.1.1.1 100.100.100.100 219.142.53.100
YES YES NO YES NO
代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>
using namespace std;
typedef struct IPadress
{
int ad[4];
int mask;
}IPAD;
bool compIP(IPAD IP, IPAD IPtest)//判断IP地址是否合法
{
int shift_non = IP.mask/8, shift_fin = IP.mask%8, i;//shift_non 无需移位,shitf_fin需要移位
bool is_matched = true;
for(i=0;i<shift_non;i++)
{
if(IP.ad[i] != IPtest.ad[i])
{
is_matched = false;
return is_matched;
}
}
if(shift_fin != 0)//比较子网掩码最后几位
{
int a = IP.ad[i] >> (8-shift_fin);
int b = IPtest.ad[i] >> (8-shift_fin);
is_matched = a&b? true:false;
}
return is_matched;
}
int main()
{
/*
allow 1.2.3.4/30
deny 1.1.1.1
allow 127.0.0.1
allow 123.234.12.23/3
deny 0.0.0.0/0
1.2.3.4
1.2.3.5
1.1.1.1
100.100.100.100
219.142.53.100
*/
int N , M;
cin >> N >> M;
vector<IPAD> IP;
vector <IPAD> IPtest;
for(int i=0;i<N;i++)
{
char pass[10],mask_flag;
int ip_1, ip_2, ip_3, ip_4, mask = 32;
scanf("%s",pass);
scanf("%d.%d.%d.%d",&ip_1,&ip_2,&ip_3,&ip_4);
mask_flag = getchar();
if(mask_flag == '/')
scanf("%d",&mask);
if(pass[0] == 'a')
{
IPAD ip;
ip.ad[0] = ip_1;
ip.ad[1] = ip_2;
ip.ad[2] = ip_3;
ip.ad[3] = ip_4;
ip.mask = mask;
IP.push_back(ip);
}
}
for(auto i=IP.begin();i!=IP.end();i++)
{
cout << (*i).ad[0] << "." << (*i).ad[1] << "." << (*i).ad[2] << "." << (*i).ad[3] << "/" << (*i).mask << endl;
}
for(int i=0;i<M;i++)
{
int ip_1, ip_2, ip_3, ip_4;
scanf("%d.%d.%d.%d",&ip_1,&ip_2,&ip_3,&ip_4);
IPAD ip;
ip.ad[0] = ip_1;
ip.ad[1] = ip_2;
ip.ad[2] = ip_3;
ip.ad[3] = ip_4;
IPtest.push_back(ip);
}
for(auto i=IPtest.begin();i!=IPtest.end();i++)
{
cout << (*i).ad[0] << "." << (*i).ad[1] << "." << (*i).ad[2] << "." << (*i).ad[3] << endl;
}
for(auto i=IPtest.begin();i!=IPtest.end();i++)
{
bool flag = false;
for(auto j=IP.begin();j!=IP.end();j++)
flag = compIP(*j, *i);
cout << (flag?"YES":"NO") << endl;
}
return 0;
}