[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 ...
随机推荐
- SpringAOP 设计原理
1. 设计原理 引入了,代理模式. java 程序执行流: 如果从虚拟机的角度看,整个程序的过程就是方法的调用,我们按照方法的执行顺序,将方法调用成一串. 在方法之间有着Join Point 连接点 ...
- div+css实现几种经典布局的详解
一.左右两侧,左侧固定宽度200px,右侧自适应占满 <div class="divBox"> <div class="left">&l ...
- CPP-基础:单目运算符重载
关于++运算符前置和后置重载的实现实例: #include <iostream> using namespace std; //创建时钟类 class Clock { public: Cl ...
- 给 MSYS2 添加国内源
https://wiki.qt.io/MSYS2pacman -S base-devel git mercurial svn wget p7zip软件包 开发包 http://mirrors.ustc ...
- 玩4K必备知识:HDMI1.4、2.0、2.0a、2.0b接口参数对比【扫盲贴】
https://www.4k123.com/thread-55369-1-1.html 前言:玩4K的同学都知道,HDMI接口是视频传输最常用的接口,但是这个接口却有好几个版本HDMI1.4.HDMI ...
- tp5 -- 微信公众号支付
近来期间比较忙, 忙完之后发现最近有挺多的东西没有整理,于是乎.就将以前用到的一些小东西整理了一下. 如果对您有帮助,则是我最大的幸运. 本篇主要是说了一下整合TP5的微信公众号支付. 不过由于最近T ...
- 看结果,测试?java中的String类 字符串拆分成字符串数组 判定邮箱地址 字符串比较 参数传递?
看结果1? package com.swift; class ArrayString { public static void main(String[] args) { String str = & ...
- viewDidLoad、viewWillAppear、viewWillDisappear
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil viewDidLo ...
- [LUOGU] P1828 香甜的黄油 Sweet Butter
题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...
- MySQL 查询优化之 Multi-Range Read
MySQL 查询优化之 Multi-Range Read MRR的工作原理 MRR开启与关闭 使用MRR示例 参考文档 在存储引擎中未缓存的大表,使用辅助索引的range scan检索数据, 可能会导 ...