时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描写叙述

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 1289 403 Forbidden的更多相关文章

  1. hihocoder 1288 : Font Size (微软2016校园招聘4月在线笔试)

    hihocoder 1288 笔试第一道..wa了好几次,也是无语..hihocoder错了不会告诉你失败的时候的测试集,这样有时候就很烦.. 遍历所有的字体,从min(w,h)开始逐渐变小开始遍历. ...

  2. 微软2016校园招聘4月在线笔试 A FontSize

    题目链接:http://hihocoder.com/problemset/problem/1288 分析:题目中所求的是最大的FontSize(记为S),其应该满足P*[W/S]*[H/S] > ...

  3. 微软2016校园招聘4月在线笔试 ABC

    题目链接:http://hihocoder.com/contest/mstest2016april1/problems 第一题:输入N,P,W,H,代表有N段文字,每段有ai个字,每行有⌊W/S⌋个字 ...

  4. [Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)

    传送门 #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Someti ...

  5. 微软2016校园招聘在线笔试-Professor Q's Software

    题目2 : Professor Q's Software 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Professor Q develops a new softw ...

  6. 微软2016校园招聘在线笔试第二场 题目1 : Lucky Substrings

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if and only if the number of different ch ...

  7. 微软2016校园招聘在线笔试 B Professor Q's Software [ 拓扑图dp ]

    传送门 题目2 : Professor Q's Software 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Professor Q develops a new s ...

  8. 微软2016校园招聘在线笔试 [Recruitment]

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A company plans to recruit some new employees. There are N ca ...

  9. 题目3 : Spring Outing 微软2016校园招聘在线笔试第二场

    题目3 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring outin ...

随机推荐

  1. iOS UITapGestureRecognizer手势和UIButton 以及UITabelView点击事件冲突

    一:在同一个view上加载,UITapGestureRecognizer手势,UIButton 行为,UITabelView点击事件冲突: 二:解决方式: 在UITapGesttureRecogniz ...

  2. 让Vs2013 完美支持EF6.1 Code First with Oracle 2015年12月24日更新

    本文是对下文的补充,切勿以为我是全盘复制哦 连接: http://www.cnblogs.com/wlflovenet/p/4187455.html Normal 0 7.8 磅 0 2 false ...

  3. 算法笔记_103:蓝桥杯练习 算法提高 金明的预算方案(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些 ...

  4. 剑指OFFER之二叉搜索树与双向链表(九度OJ1503)

    题目描述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 输入: 输入可能包含多个测试样例.对于每个测试案例,输入的第一行为一个数 ...

  5. Oracle,用left join 替代 exists ,not exists,in , not in,提高效率

    Not IN问题 Exists,not Exists,in,not in 例如: FROM YSHB B WHERE YSHA.code=b.code ) 等同于 DELETE A FROM YSHA ...

  6. Linux 修改终端显示bash-1.4$

    先取得root权限,然后在终端如下操作[root@host]$su -然后输入密码接着[root@host]#PS1='[\u@\H \W]\$' 你取得root权限后在,在终端命令下输入这个,一定要 ...

  7. 织梦dedecms修改include和plus重命名提高安全性防漏洞注入挂马

    织梦dedecms是新手站长使用得比较多的一个建站开源程序,正因如此,也是被被入侵挂马比较多的程序.下面就来跟大家说一下怎么重新命名dedecms的include文件夹以及plus文件夹来提高网站的安 ...

  8. Python-代码对象

    可调用的对象是python执行环境中最重要的部分,python语句,赋值,表达式,模块等,这些 对象只是构成可执行代码块的拼图的很少的一部分,而这些代码块被称为代码对象.   每个可调用的对象的核心都 ...

  9. jQuery自动加载更多程序(转)

    jQuery自动加载更多程序   1.1.1 摘要 现在,我们经常使用的微博.微信或其他应用都有异步加载功能,简而言之,就是我们在刷微博或微信时,移动到界面的顶端或低端后程序通过异步的方式进行加载数据 ...

  10. c#金额转换成中文大写金额 .Net开发Windows服务

    c#金额转换成中文大写金额   2018-08-24 转别人 c#金额转换成中文大写金额 /// <summary> /// 金额转换成中文大写金额 /// </summary> ...