微软笔试题2:403 Forbidden

本文介绍了一种基于IP地址和掩码的访问控制系统的设计与实现方案。通过对一系列预设规则进行匹配,该系统能够精确地控制特定IP地址或IP段的访问权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

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;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值