传送门

#1289 : 403 Forbidden

时间限制: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://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月在线笔试)的更多相关文章

  1. 微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 Little Hi runs a web server. Sometimes he has to deny acces ...

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

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

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

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

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

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

  5. hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)

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

  6. ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))

    http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...

  7. 微软2016校园招聘在线笔试之Magic Box

    题目1 : Magic Box 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 The circus clown Sunny has a magic box. When ...

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

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

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

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

随机推荐

  1. Django 模型ORM

    from django.db import models # Create your models here. class Book(models.Model): nid = models.AutoF ...

  2. Mybatis generator自动生成代码包括实体,dao,xml文件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  3. 初识 Hibernate

    Hibernate 框架 1.1   什么是框架? 框架是一个提供了可重用的公共结构半成品. 2.1   关于Hibernate Hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多 ...

  4. iOS 面试集锦2

    4.写一个setter方法用于完成@property (nonatomic,retain)NSString *name,写一个setter方法用于完成@property(nonatomic,copy) ...

  5. ios调试技巧

    一.概述1.掌握调试技巧,调试技术最基本,最重要的调试手段包括:单步跟踪,断点,变量观察等.单步跟踪(Step)所谓单步跟踪是指一行一行地执行程序,每执行一行语句后就停下来等待指示,这样你就能够仔细了 ...

  6. javascript设计模式(张容铭) 第14章 超值午餐-组合模式 学习笔记

    JS 组合模式更常用于创建表单上,比如注册页面可能有不同的表单提交模块.对于这些需求我们只需要有基本的个体,然后通过一定的组合即可实现,比如下面这个页面样式(如图14-2所示),我们来用组合模式实现. ...

  7. 文件操作-cp

    Linux cp命令 也是我们在实际使用中非常常用的一个命令,主要用来复制文件.文件夹等.今天就来给大家介绍下 cp命令 的使用. 转载自 https://www.linuxdaxue.com/lin ...

  8. 编译openwrt_MT7688_hiwooya

    参考链接: 无涯论坛地址: http://www.hi-wooya.com/forum.php openwrt官网地址:https://openwrt.org/zh-cn/doc/howto/buil ...

  9. tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04

    http://blog.csdn.net/liudijiang/article/details/50555429(转) 首先得要有制作好的uboot和linux内核镜像zImage,先烧录到sd卡里, ...

  10. cs231n课程索引

    课程资源 课程官网 课程视频-youtube 课程视频-字幕版 官方笔记 官方笔记-中文版 课程作业参考答案