时间限制: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 APP 上传

    原地址:http://www.cnblogs.com/uvsjoh/archive/2012/11/14/2769739.html 流程:1 开发好要发布的程序 -- 需要在程序中包含符合要求规格的i ...

  2. OpenERP|ODOO高德地图应用

    发布时间:2015-04-06 11:01:37来源:http://www.chinamaker.net 在openerp中的fleet模块,每一个车辆都有地图应用.默认采用的是谷歌地图,但是在应用得 ...

  3. static、final修饰符、内部类

    static修饰符: static修饰符能够与属性.方法和内部类一起使用,表示静态的.类中的静态变量和静态方法能够与类名一起使用.不须要创建一个类的对象来訪问该类的静态成员. class Static ...

  4. 42、Java国际化

    简介 国际化的英文单词是Internationalization,有时检测I18N,类似于I18N还有L10N,是Location本地化的简写. Java或计划主要通过如下三个类实现 1.java.u ...

  5. nginx 反向代理做域名转发简单配置

    这里用的是nginx for windows 首先进入nginx配置文件,做以下配置: server { listen 80; server_name abc.com; location / { pr ...

  6. Android 6.0 超级简单的权限申请 (Permission)

    代码地址如下:http://www.demodashi.com/demo/13369.html 背景描述 随着Android系统的不断升级,谷歌对用户的隐私是越来越注重了,给我们开发者带来了更多的繁琐 ...

  7. Fiddler-抓取安卓手机APP请求地址

    第一步:下载神器Fiddler,下载链接: http://fiddler2.com/get-fiddler 下载完成之后,傻瓜式的安装一下了! 第二步:设置Fiddler打开Fiddler,     ...

  8. 编译ORBSLAM2 build_ros.sh,实现kinect2在ROS环境下运行ORBSLAM2

    //編譯ORBSLAM2 build_ros.sh參考:“http://www.cnblogs.com/bigzhao/p/6635770.html”1.source ~/.bashrc出現問題:ct ...

  9. Oracle 性能调优案例(代码级别)

    业务案例一: 业务:千万记录表中查询出50条符合条件的记录. 现象:oracle部署时跨机器,业务取得数据耗时10ms.造成业务性能不达标. 为了突出主题,对于异常分支,均已省略. 对于通常写法, o ...

  10. xml布局内容总结(三)--Android

    关于xml中经经常使用到边框及边框效果,在此进行一下总结. 3.border(边框及边框效果) (1)直角边框线 <LinearLayout         android:layout_wid ...