[Hihocoder 1289] 403 Forbidden (微軟2016校園招聘4月在線筆試)

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

題解:N最大100000,暴力(n^2)算法必然超時。想到用字典樹做。IP轉化爲32位二進制數,需用Long long型變量!注意題中要求返回第一個匹配的狀態,每個節點有一個Index記錄該節點是第幾個匹配ip。注意mask爲0的情況。


#include<iostream>
#include<cstdio>
#include<string>
#include<set>
#include<map>
#include<cstring>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<sstream>
#include<limits>
using namespace std;
struct TrieNode{
    TrieNode* next[2];
    int index;
    bool flag;
    bool end;
    TrieNode() {
        memset(next,0,sizeof(next));
        flag=false;
        index=-1;     //是第幾個ip
        end=false;    //終點標誌
    }
};
TrieNode* root;
int res_index;
bool res_f;
int nonExsit;  //針對都沒有匹配上的情況。如果有mask=0的情況,則有可能是true,有可能是false。這裏nonExsit=-1則說明沒有mask=0的情況。
void insert(long long tmp,int mask,bool f,int tmp_index) {                       //nonExsit=1表示allow,nonExsit=-1代表deny
    long long cnt=(long long)1<<31;               //要用Long long轉換!
    TrieNode* p=root;
    for(int i=0;i<mask;i++) {
        int num;
        if((tmp&cnt)==cnt) num=1;
        else num=0;
        if(p->next[num]==NULL) {
            p->next[num]=new TrieNode();
        }
        p=p->next[num];
        cnt>>=1;
    }
    if(p->index==-1) {
         p->index=tmp_index;
         p->flag=f;
    }
    p->end=true;
}
bool search(long long tmp) {
    TrieNode* p=root;
    long long cnt=(long long)1<<31;
    for(int i=0;i<32;i++) {
        int num;
        if((tmp&cnt)==cnt) num=1;    //每一位是0還是1
        else num=0;
        if(p->next[num]==NULL) {
            break;
        }
        if(p->next[num]->end) {
            if(res_index==-1||p->next[num]->index<res_index) {   //取最小的Index狀態,代表最先匹配的
                res_index=p->next[num]->index;
                res_f=p->next[num]->flag;
            }
        }
        p=p->next[num];
        cnt>>=1;
    }
    if(res_index==-1) {
        if(nonExsit!=0)
           return true;
        else return false;
    }
    else return res_f;
}
int main() {
    int n,m;
    scanf("%d%d",&n,&m);
    nonExsit=-1;
    char ss[20];
    char ip[101];
    root=new TrieNode();
    for(int i=0;i<n;i++) {
        scanf("%s",ss);
        long long a,b,c,d;
        scanf("%lld.%lld.%lld.%lld",&a,&b,&c,&d);
        char cc=getchar();
        int mask=32;
        if(cc=='/') scanf("%d",&mask);
        if(mask==0) {
            if(nonExsit==-1) {
                if(strcmp(ss,"allow")==0) nonExsit=1;
                else nonExsit=0;
            }
        }
        long long tmp=(a<<24)|(b<<16)|(c<<8)|d;
        if(strcmp(ss,"allow")==0) {
            insert(tmp,mask,true,i);
        }
        else {
            insert(tmp,mask,false,i);
        }
    }
    for(int i=0;i<m;i++) {
        long long a,b,c,d;
        scanf("%lld.%lld.%lld.%lld",&a,&b,&c,&d);
        long long tmp=(a<<24)|(b<<16)|(c<<8)|d;
        res_index=-1;
        bool f=search(tmp);
        if(f) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章