微軟2016校園招聘4月線上筆試 hihocoder 1289 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 with 10000000011111110000100001111101 (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
題目連結:http://hihocoder.com/problemset/problem/1289
題目大意:給n個CIDR,查詢m個IP的請求是否被接受,按順序第一個匹配的狀態為答案,若均不匹配,則預設被接受
題目分析:01字典樹,建樹的時候要記錄輸入的順序且對於IP相同但字首不同的CIDR只取最早出現的,每次查詢沿著字典樹一直走,取路徑上最早輸入的匹配狀態為答案,還要記錄是否有字首,插入時據此來決定插入的位數,比如0.0.0.0和0.0.0.0/0是不同的
#include <cstdio>
#include <cstring>
#define ll long long
int const MAX = 4e6;
char ch, tp[10];
int n, m, num, a1, a2, a3, a4;
bool flag;
struct Trie
{
int root, tot, next[MAX][2], end[MAX], id[MAX];
inline int Newnode()
{
memset(next[tot], -1, sizeof(next[tot]));
return tot ++;
}
inline void Init()
{
memset(id, 0, sizeof(id));
tot = 0;
root = Newnode();
}
inline void Insert(ll x, int no)
{
int p = root, tmp = flag ? 31 - num : -1;
for(int i = 31; i > tmp; i--)
{
int idx = ((1ll << i) & x) ? 1 : 0;
if(next[p][idx] == -1)
next[p][idx] = Newnode();
p = next[p][idx];
}
if(!id[p])
{
id[p] = no;
end[p] = (strcmp(tp, "allow") == 0);
}
}
inline int Search(ll x)
{
bool ans = true;
int p = root, curid = 1000000;
if(id[p])
{
curid = id[p];
ans = end[p];
}
for(int i = 31; i >= 0; i--)
{
int idx = ((1ll << i) & x) ? 1 : 0;
if(next[p][idx] == -1)
return ans;
p = next[p][idx];
if(id[p] && id[p] < curid)
{
curid = id[p];
ans = end[p];
}
}
return ans;
}
}tr;
int main()
{
tr.Init();
ll b;
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++)
{
scanf("%s %d.%d.%d.%d", tp, &a1, &a2, &a3, &a4);
scanf("%c", &ch);
flag = (ch == '/');
if(flag)
scanf("%d", &num);
b = (a1 << 24) + (a2 << 16) + (a3 << 8) + a4;
tr.Insert(b, i + 1);
}
while(m --)
{
scanf("%d.%d.%d.%d", &a1, &a2, &a3, &a4);
b = (a1 << 24) + (a2 << 16) + (a3 << 8) + a4;
printf("%s\n", tr.Search(b) ? "YES" : "NO");
}
}
相關文章
- 微軟2016校園招聘4月線上筆試 hihocoder 1288 Font Size (模擬)微軟筆試
- 微軟2016校園招聘4月線上筆試 hihocoder 1290 Demo Day (dp)微軟筆試
- 各大IT公司2012校園招聘筆試面試整理筆試面試
- 華為2016校園招聘上機筆試題:簡單錯誤記錄 [python]筆試Python
- 2016微軟4月線上筆試題微軟筆試
- 校園招聘的思考
- dss nginx 403 forbiddenNginxORB
- 【JAVA】【華為校園招聘筆試-軟體】2020-09-09Java筆試
- 【校園招聘】一道筆試題看c++中virtual覆蓋筆試C++
- 百度2011.10.16校園招聘會筆試題筆試
- 【JAVA】騰訊2021校園招聘-後臺&綜合-第二次筆試Java筆試
- 百度2013校園招聘筆試題[軟體研發]及答案筆試
- 微軟windows 7推向校園微軟Windows
- Heroku 403 Forbidden Procfile 配置未生效ORB
- 【小米集團校園招聘】射頻測試工程師 (急招)工程師
- [小米集團校園招聘] 射頻測試工程師 (急招)工程師
- [廣州、成都、武漢] 多益網路22屆校園招聘 程式類10+崗位(直通筆試!)筆試
- 2014百度校園招聘筆試題(上海軟體研發工程師)筆試工程師
- svn 提交時報錯403 Forbidden 解決方法ORB
- urllib.error.HTTPError:HTTPError403:ForbiddenErrorHTTPORB
- 解決圖片訪問403 Forbidden問題ORB
- 華為2018屆校園招聘筆試題目以及相應程式碼分享 軟體開發崗位筆試
- 【資料庫】網易校園招聘筆試題--資料庫事務的定義,屬性,以及隔離級別資料庫筆試
- 爬蟲之遇到403 Forbidden,IP被封,怎麼辦?爬蟲ORB
- 【演算法】2013創新工場校園招聘筆試題--如何求出帶環連結串列的入口節點演算法筆試
- 【小米集團校園招聘】IoT 軟體專案經理
- [小米集團校園招聘] IoT 軟體專案經理
- 【小米集團校園招聘】iOS 開發工程師 (急招)iOS工程師
- 【小米集團校園招聘】嵌入式工程師 (急招)工程師
- [小米集團校園招聘] 嵌入式工程師 (急招)工程師
- [小米集團校園招聘] iOS 開發工程師 (急招)iOS工程師
- 校園網-真實的校園網路社群
- 【小米集團校園招聘】Android 開發工程師 (急招)Android工程師
- [小米集團校園招聘] Android 開發工程師 (急招)Android工程師
- 百度2015校園招聘面試題(成功拿到offer)面試題
- 處理SAP HANA Web-Based Development Workbench的403 Forbidden錯誤WebdevORB
- apache 經常無法訪問目錄列表,顯示 403 forbiddenApacheORB
- nginx靜態資原始檔無法訪問,403 forbidden錯誤NginxORB