微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden
- 例子输入
-
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
描写叙述
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.
题目链接: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 1289 403 Forbidden的更多相关文章
- hihocoder 1288 : Font Size (微软2016校园招聘4月在线笔试)
hihocoder 1288 笔试第一道..wa了好几次,也是无语..hihocoder错了不会告诉你失败的时候的测试集,这样有时候就很烦.. 遍历所有的字体,从min(w,h)开始逐渐变小开始遍历. ...
- 微软2016校园招聘4月在线笔试 A FontSize
题目链接:http://hihocoder.com/problemset/problem/1288 分析:题目中所求的是最大的FontSize(记为S),其应该满足P*[W/S]*[H/S] > ...
- 微软2016校园招聘4月在线笔试 ABC
题目链接:http://hihocoder.com/contest/mstest2016april1/problems 第一题:输入N,P,W,H,代表有N段文字,每段有ai个字,每行有⌊W/S⌋个字 ...
- [Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)
传送门 #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Someti ...
- 微软2016校园招聘在线笔试-Professor Q's Software
题目2 : Professor Q's Software 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Professor Q develops a new softw ...
- 微软2016校园招聘在线笔试第二场 题目1 : Lucky Substrings
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if and only if the number of different ch ...
- 微软2016校园招聘在线笔试 B Professor Q's Software [ 拓扑图dp ]
传送门 题目2 : Professor Q's Software 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Professor Q develops a new s ...
- 微软2016校园招聘在线笔试 [Recruitment]
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A company plans to recruit some new employees. There are N ca ...
- 题目3 : Spring Outing 微软2016校园招聘在线笔试第二场
题目3 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring outin ...
随机推荐
- js中,{}初始化数据类型object;for in 的用法;delete的用法
var choices = {}; //此数据表示的是:object{} for(var i=0;i<10;i++){ choices[i+1] = [data[i].testPlan,test ...
- iOS 证书管理.p12文件不能导出
iOS证书不能导出p12文件: 首先要确认证书是从你这个电脑上制作生成的! 钥匙串-->我的证书--->右键,就可以导出了!
- STL源代码剖析 容器 stl_map.h
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie map ------------------------------------------ ...
- 老项目转为maven的步骤具体说明
可先阅读 关于已有项目转为maven的一点看法 新建maven项目要点 事实上之前已转过几个.但忘了记录下来.今天又转了一个项目,补记录一下. 步骤 1.写pom.xml 最耗费时间的一步.由于不用m ...
- hp-ux 集群,内存 小记
-----查看hp 集群状态信息 # cmviewcl -v CLUSTER STATUS dbsvr up NODE ...
- (三)Solr——Solr的基本使用
1. Schema.xml 在schema.xml文件中,主要配置了solrcore的一些数据信息,包括Field和FieldType的定义等信息,在solr中,Field和FieldType都需要先 ...
- ubuntu下运行第一个.net core web程序
前置条件 ubuntu系统 且已经安装dotnetcore运行环境 mkdir testMVC 创建一个文件夹 cd testMVC 进入文件夹 dotnet new -t web 创建程序( ...
- mongodb - 查看集合的状态
#查看集合postalCodes的状态信息 > db.postalCodes.stats(1024) #1024表示显示的单位是KB.默认是bytes { "ns" : &q ...
- 【VBA研究】利用DateAdd函数取上月或上年同期的日期
作者:iamlaosong DateAdd函数返回一个日期.这一日期加上了一个时间间隔.通过这个函数能够计算非常多我们须要的日期,比方上月上年同期日期等. 语法 DateAdd(interval, n ...
- ManipulationStarted,ManipulationCompleted,ManipulationDelta
一.获取某个元素相对另一元素的相对位置 1.使用TransformToVisual获取某个元素相对于另外一个元素的偏移量. 例如:要获得rect相对于LayoutRoot的偏移量,就将LayoutRo ...