[Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)
#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://blog.csdn.net/zqh_1991/article/details/51103367
以及
http://paste.ubuntu.net/15664623/ 田神的代码
题解:N最大100000,暴力(n^2)算法必然超时。想到用字典树做。IP转化为32位二进制数,需用Long long型变量!注意题中要求返回第一个匹配的状态,每个节点有一个Index记录该节点是第几个匹配ip。注意mask为0的情况。
note:
1)注意题中要求返回第一个匹配的状态,每个节点有一个id[i]记录该节点是第几个匹配ip。
2)对于allow和deny两种状态,不需要建2颗字典树,用end[i]区分allow和deny即可。
3)ip会超int,要用long long
4)可以用位运算,获取ip的每一位数字
1289 | 403 Forbidden | AC | G++ | 970ms | 53MB | 2分钟前 | 查看 |
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring> int const N = 4e6;
#define ll long long
#define inf 0x3ffffff using namespace std; struct Trie
{
int root,tot;
int next[N][]; //下一个节点编号
int end[N]; //0表示末尾为deny
int id[N]; //第几个ip
int NewNode()
{
memset(next[tot],-,sizeof(next[tot]));
end[tot] = -;id[tot] = -;
return tot++;
}
void ini()
{
memset(id,-,sizeof(id));
tot = ;
root = NewNode();
}
void insert(ll x,int mask,int end_tmp,int id_tmp) //mask,匹配位数
{
int p = root;
ll cnt = 1LL << ;
for(int i = ;i < mask;i++){
int digit;
if( (x & cnt) == cnt ) digit = ;
else digit = ;
if(next[p][digit] == -){
next[p][digit] = NewNode();
}
p = next[p][digit];
cnt>>=;
}
if(id[p] == -){ //要判断,防止被后面的 rule替换了
id[p] = id_tmp;
end[p] = end_tmp;
}
}
int search(ll x)
{
int p = root;
int res_id = inf;
int res_end = -;
ll cnt = 1LL << ;
if(id[p] != -){
res_id = id[p];
res_end = end[p];
}
for(int i = ;i < ;i++){
int digit;
if( (x & cnt) == cnt ) digit = ;
else digit = ;
if(next[p][digit] == -){
break;
}
p = next[p][digit];
if(id[p] != - && id[p] < res_id){ //要判断,选取最小的id
res_id = id[p];
res_end = end[p];
}
cnt>>=;
}
return res_end;
}
}tr; char s[]; int main()
{
int n,m;
//freopen("in.txt","r",stdin);
tr.ini();
int a1, a2, a3, a4;
char ch;
ll x;
int mask;
scanf("%d %d", &n, &m);
int tmp_end;
for(int i = ; i <= n; i++)
{
scanf("%s %d.%d.%d.%d", s, &a1, &a2, &a3, &a4);
mask = ;
if(strcmp(s,"allow")==) tmp_end = ;
else tmp_end = ;
scanf("%c", &ch);
int flag = (ch == '/');
if(flag)
scanf("%d", &mask);
x = (a1 << ) + (a2 << ) + (a3 << ) + a4;
tr.insert(x,mask,tmp_end,i);
}
while(m --)
{
scanf("%d.%d.%d.%d", &a1, &a2, &a3, &a4);
x = (a1 << ) + (a2 << ) + (a3 << ) + a4;
printf("%s\n", tr.search(x) ? "YES" : "NO");
} }
[Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)的更多相关文章
- 微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 Little Hi runs a web server. Sometimes he has to deny acces ...
- 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 微软编程笔试第二题)
#1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Sometimes ...
- ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))
http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...
- 微软2016校园招聘在线笔试之Magic Box
题目1 : Magic Box 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 The circus clown Sunny has a magic box. When ...
- 微软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 ...
随机推荐
- HTTP协议详解-基础知识
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.绝大多数的Web开发,都是构建在HTTP协议之上的Web应用. HTTP协议的主要特点可概括如下: 简单: ...
- AOP日志组件 多次获取post参数
AOP日志组件 多次获取post参数 需求:新增接口日志组件.通过拦截器对接口URL进行拦截处理,然后将接口post请求的参数与结果,写入日志表. 问题:POST方法的参数是存储在request.ge ...
- Web开发面临的挑战主要有哪些?
摘要:要成为一名高效的Web开发者,这需要我们做很多工作,来提高我们的工作方式,以及改善我们的劳动成果.而在开发中难免会遇到一些困难,从前端到后端. 导读:要成为一名高效的Web开发者,这需要我们做很 ...
- vue axios 请求本地接口端口不一致出现跨域设置代理
首先在config下面的index.js,设置跨域代理 在axios请求的时候 用'/api/' 替代baseURL 最重要的就是设置完必须重新 npm run dev 否则不生效
- Luogu P1080国王游戏(贪心)
国王游戏 题目链接:国王游戏 ps:题目数据说明了要写高精度. 这个题的答案是\(a.l * a.r < b.l * b.r\)按照这个进行排序 题解中大部分只是如何证明排序是: \(a.l * ...
- 【Linux】VirtualBox虚拟网络配置
Host OS : Windows 10 Guest OS : CentOS 6.8 VirtualBox:5.1.18 网络连接方式: NAT 1.CentOS中使用DHCP [root@gouka ...
- php数据查询之基础查询
---恢复内容开始--- 数据查询语言(Data Query Language) 基本查询 语法形式: select [all | distinct ] 字段或者表达式列表 [from子句] [whe ...
- laravel富文本编辑和图片上传
---恢复内容开始--- 首先先找到一个适合的编辑器是胜利的一步,选择wangEditor这个编辑器 地址:http://www.wangeditor.com/ 然后选择下载,我是通过网上学习的,所以 ...
- 【java】抽象类继承关系
抽象类: 抽象类不能用来实例化对象,声明抽象类的唯一目的是为了将来对该类进行扩充. 一个类不能同时被 abstract 和 final 修饰.如果一个类包含抽象方法,那么该类一定要声明为抽象类,否则将 ...
- laravel 设计思想简单了解
服务容器 laravel框架中 服务容器是整个系统功能调度配置的核心,在系统运行过程中动态的为系统提供需要的服务 从而实现了解耦 控制反转(IOC) 控制反转是一种设计模式 主要解决了系统组件之间的相 ...