#1289 : 403 Forbidden
-
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
描述
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.
后来,总结就是一到这样的笔试,自己就兴奋,有好处也有坏处,代码极其混乱,心态极其不稳定。但终究其原因,还是因为自己基础不扎实,太弱了。
后来对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;
}