403 Forbidden@hihoCoder 1289

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.

Input

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.

Output

For each request output "YES" or "NO" according to whether it is allowed.

Sample Input

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

Sample Output

YES
YES
NO
YES
NO

题目分析:这种面试题含金量都很高。这题最好先去了解一下IP地址和子网掩码是什么,如果你了解过这两个东西,你就会很自然的想到用二进制表示,然后这就是一个突破口。用二进制的话就可以选择用Trie解决了,题中要注意优先级的问题。

这种题很考察细心和耐心(唉,说多了都是泪啊),还有后来我看到别人的题解用sscanf处理字符串很方便,可以先去了解一下。

这个题我提交了三四遍都没过,然后从别人的博客里翻到了几组分享的测试样例,通过这几组样例又修正了自己的代码,然后就通过了。这种大神真的厉害。把这几组样例偷过来供大家分享,嘿嘿。原博主博客

5 2
deny 0.0.0.0/0
allow 0.0.0.0/0
deny 0.0.0.0/1
deny 0.0.0.0/2
allow 123.234.12.23/3
123.234.12.23
0.234.12.23
答案: NO NO
2 1
deny 1.1.1.1
allow 127.0.0.1
1.1.1.222
答案:YES

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
#define MAXN 1000003
using namespace std;

int trie[MAXN][2];
int k;
int level;
struct Color{ //终结点结构体
    int c; //标记是不是终结点,1是,0不是
    bool flag; //标记这个终结点代表allow还是deny,true代表allow
    int level; //标记这个终结点的优先级,0最大
}color[MAXN];
char s[50];
bool flag; //临时记录这个IP地址属于allow还是deny
struct Bin{ //二进制串
    int num[35]; //存放IP地址转换后的二进制串
    int len; //记录这个串需要用到的长度
}bin;

void solve(){
    bin.len = 0;
    int len = strlen(s);
    int num = 0;
    int pos = 0; //pos位置记录'/'后第一个数字字符的位置,如果没有'/',pos=0
    for(int i=0;i<len;++i){
        if(!(s[i] >= '0' && s[i] <= '9')){
            int cnt = 8;
            while(cnt--){
                int val = num & 1;
                num >>= 1;
                bin.num[bin.len+cnt] = val;
            }
            bin.len += 8;
            num = 0;
            if(s[i] == '/'){
                pos = i+1;
                break;
            }
        }
        else num = num * 10 + (s[i] - '0');
    }
    if(!pos){
        int cnt = 8;
        while(cnt--){
            int val = num & 1;
            num >>= 1;
            bin.num[bin.len+cnt] = val;
        }
        bin.len += 8;
    }
    else{
        num = 0;
        for(int i=pos;i<len;++i){
            num = num * 10 + (s[i] - '0');
        }
        bin.len = num;
    }
}

void Insert(){
    int p = 0;
    for(int i=0;i<bin.len;++i){
        if(!trie[p][bin.num[i]]) trie[p][bin.num[i]] = ++k;
        p = trie[p][bin.num[i]];
    }
    if(level < color[p].level){
        color[p].level = level++;
        color[p].c = 1;
        color[p].flag = flag;
    }

}

bool Search(){
    int p = 0;
    bool ans = color[0].flag;
    int nowl = color[0].level;
    for(int i=0;i<bin.len;++i){
        if(!trie[p][bin.num[i]]){
            return ans;
        }
        p = trie[p][bin.num[i]];
        if(color[p].c == 1 && color[p].level < nowl){
            ans = color[p].flag;
            nowl = color[p].level;
        }
    }
    return ans;
}

int main(){
    int n,m;
    char op[10];
    while(~scanf("%d%d",&n,&m)){
        memset(trie,0,sizeof(trie));
        k = 0;
        level = 0;
        for(int i=0;i<MAXN;++i){
            color[i].c = 0;
            color[i].flag = true;
            color[i].level = MAXN;
        }
        for(int i=0;i<n;++i){
            scanf("%s%s",op,s);
            if(op[0] == 'a') flag = true;
            else flag = false;
            solve(); //对字符串的处理,建议去了解一下sscanf,处理起来很方便
            /*cout << bin.len << endl;
            for(int i=0;i<32;++i){
                cout << bin.num[i];
                if((i+1)%8 == 0) cout << ",";
            }
            cout << endl;*/
            Insert();
        }
        for(int i=0;i<m;++i){
            scanf("%s",s);
            solve();
            /*cout << bin.len << endl;
            for(int i=0;i<bin.len;++i){
                cout << bin.num[i];
                if((i+1)% 8 == 0) cout << ",";
            }
            cout << endl;*/
            if(Search()) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39021458/article/details/81234973