2016微软在线编程2:403 Forbidden

描述

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 <bits/stdc++.h>
using namespace std;

int main()
{
	int n, m;
	map<int, bool>ma[33];
	cin >> n >> m;
	int** rule = new int*[n];
	for (int i = 0; i < n; i++)
	{
		rule[i] = new int[5];
	}
	bool* choice = new bool[n];
	for (int i = 0; i < n; i++)
	{
		string input;
		cin >> input;
		if (input == "allow")
		{
			choice[i] = true;
		}
		else
		{
			choice[i] = false;
		}
		rule[i][4] = 32;
		scanf("%d.%d.%d.%d/%d", &rule[i][0], &rule[i][1], &rule[i][2], &rule[i][3], &rule[i][4]);
		int totalip1;
		char *p1 = (char*)&totalip1;
		if (rule[i][4] != 0)
		{
			p1[0] = rule[i][3];
			p1[1] = rule[i][2];
			p1[2] = rule[i][1];
			p1[3] = rule[i][0];
			totalip1 = totalip1 >> (32 - rule[i][4]);
		}
		else
		{
			totalip1 = 0;
		}
		ma[rule[i][4]].insert(pair<int, bool>(totalip1, choice[i]));
	}


	int** ip = new int*[m];
	for (int i = 0; i < m; i++)
	{
		ip[i] = new int[4];
	}
	for (int i = 0; i < m; i++)
	{
		scanf("%d.%d.%d.%d", &ip[i][0], &ip[i][1], &ip[i][2], &ip[i][3]);
		bool flag = true;
		int totalip2;
		char *p2 = (char*)&totalip2;
		p2[0] = ip[i][3];
		p2[1] = ip[i][2];
		p2[2] = ip[i][1];
		p2[3] = ip[i][0];
		for (int j = 32; j >= 0; j--)
		{
			int tmp = totalip2;
			if (j != 0)
			{
				tmp = tmp >> (32 - j);
			}
			else
			{
				tmp = 0;
			}
			if (ma[j].find(tmp) != ma[j].end())
			{
				if (ma[j].find(tmp)->second == false)
				{
					flag = false;
				}
				break;
			}
		}
		if (flag)
		{
			cout << "YES" << endl;
		}
		else
		{
			cout << "NO" << endl;
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值