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;
}

相關推薦

微軟試題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 stil

hihocoder 1289 微軟2016校園招聘4月線上筆試-2:403 Forbidden

http://hihocoder.com/problemset/problem/1289?sid=763673 時間限制: 10000ms 單點時限: 1000ms 記憶體限制: 256MB

微軟2016校園招聘4月線上筆試2-403 Forbidden

時間限制:10000ms 單點時限:1000ms 記憶體限制:256MB 描述 Little Hi runs a web server. Sometimes he has to deny access from a certain set of mali

軟件測試試題2

介紹 app 求職 == vim java接口 http cookie rmdir 1、線程和進程區別 2、死鎖 3、接口和抽象類區別 4、數組和鏈表的區別 5、左連接和右連接區別 個人優缺點、求職意向、自我介紹、軟件測試深入 理論、流程、方法(簡單實用 工具 jmeter

數據庫試題2

bsp blank 筆試 target word net 產生 四大 ref 事務隔離(isolation)定義了數據庫系統中一個操作產生的影響什麽時候以哪種方式可以對其他並發操作可見。隔離是事務ACID (原子性、一致性性、隔離性、持久性)四大屬性中的一個重要屬性。 邏輯

Java基礎階段試題2

return throws 行處理 錯誤 try ray 一段 內存操作 ica Java基礎階段筆試題2 問題:Collection 和 Collections的區別?(10分) 本題目的:檢測對面結合類的理解 答案: Collection是集合類的上級接口(

華為2018年4月實習試題2 數字跳躍

ring 開始 一個數 new ID next n) div 表示 第一行表示有多少個數n第二行開始依次是1到n個數,一個數一行輸出描述:輸出一行,表示最少跳躍的次數。示例1輸入72321215輸出3 說明 7表示接下來要輸入7個正整數,從2開始。數字本身代表可以跳躍的最大

飛步A輪試題2 等分字串

 等分數字串 時間限制:10000ms 單點時限:1000ms 記憶體限制:256MB 描述 給定一個只包含0-9的字串S,請你判斷能否將S劃分為兩個或兩個以上連續的子串,使得每一個子串中的數字總和都相等。 輸入 輸入包含多組資料。 第一行包含一個整數N,代

Java試題——2的100次方,不用大資料類(Biginteger)來解答

Java筆試題——2的100次方,不用大資料類(Biginteger)來解答 package cn.hncu.offer; public class Two100 { public static void main(String[] args) { int a[]=new int[1];//

試題2

一、填空題(每空1分) 總分:108分 1.HTML作用是_____。CSS作用是_____。JS作用是_____。 html是通過標籤搭建頁面結構; css是通過樣式美化頁面; js是給頁面新增動態功能。 2.$(“span”).text()含義____。$

白話經典算法系列之九 從歸併排序到數列的逆序數對(微軟試題

首先來看看原題   微軟2010年筆試題 在一個排列中,如果一對數的前後位置與大小順序相反,即前面的數大於後面的數,那麼它們就稱為一個逆序數對。一個排列中逆序的總數就稱為這個排列的逆序數。如{2,4,3,1}中,2和1,4和3,4和1,3和1是逆序數對,因此整個陣列的逆序數對個數為

微軟試題——基數排序

這是一題微軟的筆試題,原題如下:  排序N個比N^7小的數,要求的演算法是O(n)(給了提示..說往N進位制那方面想) 此題的分析見 http://blog.csdn.net/sinshine/article/details/6844370 本人使用了N進位制的做法,當然

京東試題2:求括號匹配數目

題目描述: 描述 有一個合法的字串,合法是指左括號與右括號全部能配對,現在每次將這個序列第一個左括號刪去,在將任意一個右括號刪去,每次刪去後的序列必須合法,求有多少種方法 輸入 一個合法括號序列。 輸出 方案數。 樣例輸入 (

微軟試題 大型檔案外部排序(二路歸併和k路歸併的實現和比較)

這兩種排序方法都是先將一個無序的大的外部檔案,分成若干塊,分別讀到記憶體中。 將每一塊都先排好序,放到一個新的外部檔案中。 二路歸併的思路是每次將外部排序的檔案兩兩合併,變成一個二倍大小的檔案,然後對二倍大小的檔案繼續兩兩合併。直到最終合併為一個檔案為止。k路歸併是將外部排

Web前端面試試題2——JS(1):函式呼叫(區域性變數/全域性變數)

1、函式呼叫——區域性變數/全域性變數 案例1-1:fun(a)函式有傳參,即可更改“區域性變數” var a=100; function fun(a){ a++; consol

經典基礎C++試題2(附答案)

1. C中static有什麼作用?(1)隱藏。 當我們同時編譯多個檔案時,所有未加static字首的全域性變數和函式都具有全域性可見性,故使用static在不同的檔案中定義同名函式和同名變數,而不必擔心命名衝突。(2)static的第二個作用是保持變數內容的持久。儲存在靜態資

2017年華為實習生招聘試題2

題目如圖片中所示,程式碼如下: #include "stdafx.h" #include <stdio.h> #include <tchar.h> #include <iostream> int state[6] = {1

TOP K演算法(微軟試題 統計英文電子書中出現次數最多的k個單詞)

        在v_JULY_v的文章中找到了這個問題的解法後用C++實現了一下,發現C++的程式碼非常的簡潔。 主要用到了標準庫中的hash_map,優先順序佇列priority_queue。  

騰訊試題 2 有趣的數字

小Q今天在上廁所時想到了這個問題:有n個數,兩兩組成二元組,差最小的有多少對呢?差最大呢? 輸入描述: 輸入包含多組測試資料。 對於每組測試資料: N - 本組測試資料有n個數 a1,a2...an - 需要計算的資料 保證: 1<=N<=100000,0<

網易機器學習試題2

D : p = &x;  表示的是,對x取地址,賦值給指標p,那麼p 將指向 x 的那塊記憶體空間,但是 x  是形式引數(也有人說是方法引數,都可以),函式呼叫完了之後,記憶體就釋放了,所以再返回 *p(即取出那塊記憶體空間的值),已經找不到了。所以錯誤。 A