微软2016校园招聘4月在线笔试 hihocoder 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 with 10000000011111110000100001111101 (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

题目链接:http://hihocoder.com/problemset/problem/1289

题目大意:给n个CIDR,查询m个IP的请求是否被接受,按顺序第一个匹配的状态为答案,若均不匹配。则默认被接受

题目分析:01字典树。建树的时候要记录输入的顺序且对于IP同样但前缀不同的CIDR仅仅取最早出现的。每次查询沿着字典树一直走。取路径上最早输入的匹配状态为答案。还要记录是否有前缀,插入时据此来决定插入的位数,比方0.0.0.0和0.0.0.0/0是不同的

#include <cstdio>
#include <cstring>
#define ll long long
int const MAX = 4e6;
char ch, tp[10];
int n, m, num, a1, a2, a3, a4;
bool flag;

struct Trie  
{  
    int root, tot, next[MAX][2], end[MAX], id[MAX];  
    inline int Newnode()  
    {  
        memset(next[tot], -1, sizeof(next[tot]));  
        return tot ++;  
    }  
  
    inline void Init()  
    {  
    	memset(id, 0, sizeof(id));
        tot = 0;  
        root = Newnode();  
    }  
  
    inline void Insert(ll x, int no)  
    {  
        int p = root, tmp = flag ?

31 - num : -1; for(int i = 31; i > tmp; i--) { int idx = ((1ll << i) & x) ? 1 : 0; if(next[p][idx] == -1) next[p][idx] = Newnode(); p = next[p][idx]; } if(!id[p]) { id[p] = no; end[p] = (strcmp(tp, "allow") == 0); } } inline int Search(ll x) { bool ans = true; int p = root, curid = 1000000; if(id[p]) { curid = id[p]; ans = end[p]; } for(int i = 31; i >= 0; i--) { int idx = ((1ll << i) & x) ? 1 : 0; if(next[p][idx] == -1) return ans; p = next[p][idx]; if(id[p] && id[p] < curid) { curid = id[p]; ans = end[p]; } } return ans; } }tr; int main() { tr.Init(); ll b; scanf("%d %d", &n, &m); for(int i = 0; i < n; i++) { scanf("%s %d.%d.%d.%d", tp, &a1, &a2, &a3, &a4); scanf("%c", &ch); flag = (ch == '/'); if(flag) scanf("%d", &num); b = (a1 << 24) + (a2 << 16) + (a3 << 8) + a4; tr.Insert(b, i + 1); } while(m --) { scanf("%d.%d.%d.%d", &a1, &a2, &a3, &a4); b = (a1 << 24) + (a2 << 16) + (a3 << 8) + a4; printf("%s\n", tr.Search(b) ? "YES" : "NO"); } }



posted @ 2017-08-03 18:43  wzzkaifa  阅读(112)  评论(0编辑  收藏  举报