微软2016校园招聘4月在线笔试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


代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>

using namespace std;

typedef struct IPadress
{
    int ad[4];
    int mask;
}IPAD;


bool compIP(IPAD IP, IPAD IPtest)//判断IP地址是否合法
{
    int shift_non = IP.mask/8, shift_fin = IP.mask%8, i;//shift_non 无需移位,shitf_fin需要移位
    bool is_matched = true;
    for(i=0;i<shift_non;i++)
    {
        if(IP.ad[i] != IPtest.ad[i])
        {
            is_matched = false;
            return is_matched;
        }
    }
    if(shift_fin != 0)//比较子网掩码最后几位
    {
        int a = IP.ad[i] >> (8-shift_fin);
        int b = IPtest.ad[i] >> (8-shift_fin);
        is_matched = a&b? true:false;
    }
    return is_matched;
}


int main()
{
    /*
    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
    */

    int N , M;
    cin >> N >> M;
    vector<IPAD> IP;
    vector <IPAD> IPtest;
    for(int i=0;i<N;i++)
    {
        char pass[10],mask_flag;
        int ip_1, ip_2, ip_3, ip_4, mask = 32;
        scanf("%s",pass);
        scanf("%d.%d.%d.%d",&ip_1,&ip_2,&ip_3,&ip_4);
        mask_flag = getchar();
        if(mask_flag == '/')
            scanf("%d",&mask);
        if(pass[0] == 'a')
        {
            IPAD ip;
            ip.ad[0] = ip_1;
            ip.ad[1] = ip_2;
            ip.ad[2] = ip_3;
            ip.ad[3] = ip_4;
            ip.mask = mask;
            IP.push_back(ip);
        }
    }
    for(auto i=IP.begin();i!=IP.end();i++)
    {
        cout << (*i).ad[0] << "." << (*i).ad[1] << "." << (*i).ad[2] << "." << (*i).ad[3] << "/" << (*i).mask << endl;
    }
    for(int i=0;i<M;i++)
    {
        int ip_1, ip_2, ip_3, ip_4;
        scanf("%d.%d.%d.%d",&ip_1,&ip_2,&ip_3,&ip_4);
            IPAD ip;
            ip.ad[0] = ip_1;
            ip.ad[1] = ip_2;
            ip.ad[2] = ip_3;
            ip.ad[3] = ip_4;
            IPtest.push_back(ip);
    }
    for(auto i=IPtest.begin();i!=IPtest.end();i++)
    {
        cout << (*i).ad[0] << "." << (*i).ad[1] << "." << (*i).ad[2] << "." << (*i).ad[3] << endl;
    }

    for(auto i=IPtest.begin();i!=IPtest.end();i++)
    {
        bool flag = false;
        for(auto j=IP.begin();j!=IP.end();j++)
            flag = compIP(*j, *i);
        cout << (flag?"YES":"NO") << endl;
    }
    return 0;
}





Access Error: 403 - Forbidden是一种HTTP状态码,表示请求被服务器拒绝了。这种情况通常是由于缺乏足够的权限或身份验证问题导致的。根据提供的引用内容,Access Error: 403 - Forbidden是由于请求的URL没有授权而导致的。 要解决这个问题,首先需要确保你有足够的权限来访问该URL。如果你是管理员或有相关权限的用户,请确保你已经正确地进行了身份验证。如果你不是管理员或没有相关权限,你需要联系管理员或拥有相应权限的用户来获得访问授权。 另外,还需要确保请求的URL是正确的,没有拼写错误或其他问题。你可以检查URL是否正确,并确保没有任何额外的字符或空格。 如果你确认你具有足够的权限并且URL是正确的,但仍然遇到Access Error: 403 - Forbidden错误,那么可能是服务器配置或其他安全设置导致的问题。在这种情况下,你应该联系服务器管理员或技术支持人员,向他们报告该问题,并提供尽可能多的细节和错误信息,以便他们可以帮助你解决这个问题。 总结起来,Access Error: 403 - Forbidden表示请求被服务器拒绝访问。要解决这个问题,你需要确保具有足够的权限,验证身份,并检查URL是否正确。如果问题仍然存在,请联系服务器管理员或技术支持人员以获得进一步的帮助。<span class="em">1</span> #### 引用[.reference_title] - *1* [springboot-springsecurity-jwt-demo](https://download.csdn.net/download/lxw66468843/10942120)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值