传送门

#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. HDU 6166 Senior Pan(多校第九场 二进制分组最短路)

    题意:给出n个点和m条有向边(有向边!!!!我还以为是无向查了半天),然后给出K个点,问这k个点中最近的两点的距离 思路:比赛时以为有询问,就直接丢了,然后这题感觉思路很棒,加入把所有点分成起点和终点 ...

  2. ulrlib案例-爬取百度贴吧

    1.任务需求 百度贴吧有很多主题,每个主题下的网页存在很多分页.爬取不同的主题,并下载每个主题下的多页网页. 输入贴吧名称,下载相应贴吧的多页网页,设置最多下载50页. 2.分析网页 访问不同的百度贴 ...

  3. OpenCascade:屏闪问题。

    1.在OnDraw中同时调用用V3d_View::Redaw()和 V3d_View::FitAll();可暂时解决. 2.在OnDraw中同时调用用V3d_View::Update();

  4. webuploader项目中多图片上传实例

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. shell脚本,awk实现每个数字加1.

    [root@localhost add]# cat file [root@localhost add]# cat file|awk '{for(i=1;i<=NF;i++){$i+=1}}1' ...

  6. Ruby设计模式-观察者模式学习笔记

    observer.rb #!/bin/env ruby # encoding: utf-8 require 'observer' class CriminalMovement include Obse ...

  7. [CODEVS] 2189 数字三角形W

    数字三角形 要求走到最后mod 100最大 可达性DP(好像是这样叫) 用bool数组f[i][j][k]表示 位置(i,j)能否得到k(mod 100意义下) 转移条件 f[i][j][k]=f[i ...

  8. python爬虫基础08-selenium大全2/8-Chrome Webdriver启动选项

    Selenium笔记(2)Chrome Webdriver启动选项 本文集链接:https://www.jianshu.com/nb/25338984 在Selenium中使用不同的Webdriver ...

  9. java 之Thread线程相关yield()、sleep()、wait()、join()、run和start方法详解

    1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...

  10. (转) 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程(精)

    原文地址:http://blog.csdn.net/holydancer/article/details/9219333 概述: 苹果的证书繁锁复杂,制作管理相当麻烦,今天决定重置一个游戏项目中的所有 ...