微軟2016校園招聘4月線上筆試 hihocoder 1289 403 Forbidden

_TCgogogo_發表於2016-04-07
時間限制: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");
	}
}


相關文章