时间限制: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. ThinkPHP 3.2 中获取所有函数方法名,以及注释,完整可运行

    <?php namespace Home\Controller; use Common\Controller\BaseController; class AuthController exten ...

  2. 算法笔记_070:BellmanFord算法简单介绍(Java)

    目录 1 问题描述 2 解决方案 2.1 具体编码   1 问题描述 何为BellmanFord算法? BellmanFord算法功能:给定一个加权连通图,选取一个顶点,称为起点,求取起点到其它所有顶 ...

  3. webDriver API——第14部分Color Support

    class selenium.webdriver.support.color.Color(red, green, blue, alpha=1) Bases: object Color conversi ...

  4. 01-hibernate注解:类级别注解准备工作

    注解简介: 目的:为了简化繁琐的ORM映射文件(.hbm)的配置. JPA与hibernate的关系 JPA:全称 java Persistence API(java持久化API接口) JPA注解是J ...

  5. 利用Redis撤销JSON Web Token产生的令牌

    利用Redis撤销JSON Web Token产生的令牌 作者:chszs.版权全部.未经允许,不得转载.博主主页:http://blog.csdn.net/chszs 早先的博文讨论了在Angula ...

  6. (四)Oracle学习笔记—— 常见函数

    1. 字符串类型及函数 字符类型分 种,char(n) .varchar(n).varchar2(n) : char(n)固定长度字符串,假如长度不足 n,右边空格补齐: varchar(n)可变长度 ...

  7. Eclipse项目上红叉

    整个项目可以编译通过并且运行都没问题,但是项目上有个红叉,这个问题一般是有两个原因: 1.查看项目是不是有的引用包报错 解决办法:在项目的build path 中删除不可用引用或者修正 2.项目的编译 ...

  8. WebSocket遇到的一些问题

    一 .Nginx配置websocket   为了解决Nginx转发不能进行websocket通信问题 将nginx配置文件添加如下内容:   map $http_upgrade $connection ...

  9. poj1860--Currency Exchange

    Bellman-ford算法的反向应用--正循环检查 /** \brief poj 1860 Bellman-Ford * * \param date 2014/7/24 * \param state ...

  10. DDR3内存详解,存储器结构+时序+初始化过程

    DDR3内存详解,存储器结构+时序+初始化过程 标签: DDR3存储器博客 2017-06-17 16:10 1943人阅读 评论(1) 收藏 举报  分类: 硬件开发基础(2)  转自:http:/ ...