hihocoder 1089:403 Forbidden

#1289 : 403 Forbidden

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

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
前两天刚刚写的trie树,结果笔试的时候还是把自己写爆炸了。。。

后来,总结就是一到这样的笔试,自己就兴奋,有好处也有坏处,代码极其混乱,心态极其不稳定。但终究其原因,还是因为自己基础不扎实,太弱了。

后来对trie树重新理了一下思路,这其实是一道标准的不能再标准的trie树了。就是往里面插入二进制的ip,然后查询其标记。唉。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x333f3f3f
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))

const ll mod = 100000007;
const int maxn = 1e5 + 5;
const double PI = acos(-1.0);

int cnt, n, m;
int f[32 * maxn];

struct trie
{
	int index;
	int next[2];
}tree[32 * maxn];

void init()
{
	cnt = 1;
	memset(tree, -1, sizeof(tree));
	memset(f, -1, sizeof(f));

}

void add(ll ip, int mask, int flag,int index)
{
	int now = 1;
	int i, j, k;
	for (i = 31; i >= 31 - (mask - 1); i--)
	{
		k = ((ip&(1 << i)) >> i);
		if (tree[now].next[k] == -1)
		{
			++cnt;
			tree[now].next[k] = cnt;
		}
		now = tree[now].next[k];
	}
	if (tree[now].index == -1)
	{
		tree[now].index = index;
		f[now] = flag;
	}
}

bool query(ll ip)
{
	int res = -1, now = 1, flag;
	int i, j, k;
	for (i = 31; i >= 0; i--)
	{
		if (tree[now].index != -1)
		{
			if (res == -1 || res > tree[now].index)
			{
				res = tree[now].index;
				flag = f[now];
			}
		}
		k = ((ip&(1 << i))>>i);
		if (tree[now].next[k] == -1)
			break;
		now = tree[now].next[k];
	}
	if (res == -1)
	{
		return true;
	}
	else
	{
		return flag;
	}
}

void solve()
{
	int i, j, k, mask;
	ll ip1, ip2, ip3, ip4;

	char oper, x[15];

	for (i = 1; i <= n; i++)
	{
		scanf("%s", x);
		if (x[0] == 'a')
		{
			scanf("%lld.%lld.%lld.%lld%c", &ip1, &ip2, &ip3, &ip4, &oper);
			if (oper == '/')
			{
				scanf("%d", &mask);
			}
			else
			{
				mask = 32;
			}
			ll res = (ip1 << 24) + (ip2 << 16) + (ip3 << 8) + ip4;
			add(res, mask, 1,i);
		}
		else
		{
			scanf("%lld.%lld.%lld.%lld%c", &ip1, &ip2, &ip3, &ip4, &oper);
			if (oper == '/')
			{
				scanf("%d", &mask);
			}
			else
			{
				mask = 32;
			}
			ll res = (ip1 << 24) + (ip2 << 16) + (ip3 << 8) + ip4;
			add(res, mask, 0, i);
		}
	}

	for (i = 1; i <= m; i++)
	{
		scanf("%lld.%lld.%lld.%lld", &ip1, &ip2, &ip3, &ip4);
		ll res = (ip1 << 24) + (ip2 << 16) + (ip3 << 8) + ip4;
		if (query(res))
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
}

int main()
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		init();
		solve();
	}
	
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值