1. 程式人生 > >微軟筆試題2:403 Forbidden

微軟筆試題2:403 Forbidden

描述

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
題意:就是實現windows系統中的host功能,增加了掩碼(mask)這個東西,意味著可以允許或者拒絕一段的ip地址,注意一點allow 0.0.0.0/0是允許所有通過,deny 0.0.0.0/0是拒絕所有通過
思路:就是用一個map儲存所有的規則,然後在規則中查詢就行,本題也可以用trie樹來解決,本題應該就是覺得能不能進入微軟面試環節的把關題了,AC這題,加上第一題200分的話,應該就能進面試了
程式碼如下,筆試的時候已經AC
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<time.h>
#include<math.h>
#include<memory>
#include<vector>
#include<bitset>
#include<fstream>
#include<stdio.h>
#include<utility>
#include<sstream>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
map<unsigned int,int> ma[35];
int main()
{
    
    int n,m;
    scanf("%d%d",&n,&m);
    int i;
    int x1,x2,x3,x4;
    for (i=1;i<=n;i++)
    {
        static char a[35];
        scanf("%s",a);
        scanf("%d.%d.%d.%d",&x1,&x2,&x3,&x4);
        unsigned t=(x1<<24)+(x2<<16)+(x3<<8)+x4;
        char temp;
        scanf("%c",&temp);
        int masks=32;
        if (temp=='/')
        {
            scanf("%d",&masks);
        }
        t>>=(32-masks);
        if (masks==0) t=0;
        if (a[0]=='a')
        {
            if (ma[masks][t]==0) ma[masks][t]=i;
        }
        else
        {
            if (ma[masks][t]==0) ma[masks][t]=-i;
        }
    }
    for (i=1;i<=m;i++)
    {
        scanf("%d.%d.%d.%d",&x1,&x2,&x3,&x4);
        unsigned t=(x1<<24)+(x2<<16)+(x3<<8)+x4;
        int j;
        int rules=n+1;
        for (j=0;j<=32;j++)
        {
            unsigned tx=t>>(32-j);
            if (j==0) tx=0;
            if (ma[j].find(tx)!=ma[j].end())
            {
                if (abs(ma[j][tx])<abs(rules))
                {
                    rules=ma[j][tx];
                }
            }
        }
        //printf("%d\n",rules);
        if (rules<0) puts("NO"); else puts("YES");
    }
    return 0;
}

相關推薦

no