hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)
#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题目大意:
给定N个允许接入或者拒绝的IP或者IP/掩码
再给M个IP地址,判断是否可以接入,可以输出YES,否则输出NO(没有匹配的输出YES)
1<=N,M<=100000
(判断的时候要按照N条规则的先后顺序判断的,遇到符合的就输出)
思路:
一开始没看到N和M的最大值,做法是,将N条规则中的IP转换为整数,在按照掩码位数得到掩码(没有掩码的按照掩码位数为32),
并将掩码和掩码位数存储到结构体数组中,对于测试的每一条IP,与结构体数组中的掩码,按照掩码位数进行比较,直到找到第一条
匹配的。 当然这种想法果断超时。AC思路:
为了节省查询的时间,想到用map存储N条规则,但是map ,在查找是并不是按照N条规则给的顺序给你查找啊,所以在map<pnode,node>
pnode 是一个结构体,存储的是掩码和掩码的位数,node 存储的是顺序和是否允许接入;
当我们判断一个IP是否允许接入时,我们事先并不知道,掩码位数为多少的出现在前面,所以要从0~32位逐一枚举测试IP的掩码位数,查看是否在map
,查看是否在map中出现过,若出现了,比较出现的顺序,取出现较早的#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <map>
#define maxn 110
#define inf 0x7fffffff
using namespace std;
struct node
{
int pos;// 序号
int flag;// 拒绝还是接受 1表示接受,0表示拒绝
node()
{ }
node(int x, int y)
{
pos = x;
flag = y;
}
bool operator<(const node& b)
{
return pos < b.pos;
}
};
struct pnode
{
int k;// ip 值
int num; // 子网掩码位数
pnode()
{ }
pnode(int x, int y)
{
k = x;
num = y;
} bool operator<(const pnode& b)const
{
if(k!=b.k) return k < b.k;
else
{
return num < b.num;
}
} };
map<pnode, node>my_map;
int Trans(int x,int y, int z, int w, int num)
{
int ans = 0;
if(num == 0)return ans;
ans = ans|x;
ans<<=8;
ans = ans|y;
ans<<=8;
ans = ans|z;
ans<<=8;
ans = ans|w;
int k = 32 - num;
ans>>=k;
ans<<=k; return ans; } int main()
{ //freopen("data.txt","r",stdin);
int n,m;
char str[maxn];
char c[20];
int x,y,z,w;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
my_map.clear();
for(int j = 0; j < n;j++)
{
gets(str);
int len = strlen(str);
bool f = false;
for(int i = len - 1;i >= 0;i--)
{
if(str[i] == '/')
{
f = true;
break;
}
} int num = 32;
if(f)sscanf(str, "%s %d.%d.%d.%d/%d", c, &x,&y,&z,&w,&num);
else sscanf(str, "%s %d.%d.%d.%d", c, &x,&y,&z,&w); int k = Trans(x,y,z,w,num);
int flag = 0;
pnode tmp_pnode;
tmp_pnode.k = k;
tmp_pnode.num = num;
if(strcmp(c, "allow") == 0)
{
flag = 1;
} if(my_map.find(tmp_pnode) == my_map.end())
{
my_map[tmp_pnode] = node(j,flag);
} } while(m--)
{
scanf("%d.%d.%d.%d",&x,&y,&z,&w);
int pos = inf;
int flag = 1;
for(int i = 0 ; i <= 32;i++)
{
int k = Trans(x,y,z,w,i);
pnode tmp_pnode;
tmp_pnode.k = k;
tmp_pnode.num = i; if(my_map.find(tmp_pnode)!= my_map.end())
{
node a = my_map[tmp_pnode]; if(pos > a.pos)
{
pos = a.pos;
flag = a.flag;
}
}
} if(flag == 1)puts("YES");
else puts("NO");
}
} }
hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)的更多相关文章
- [Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)
传送门 #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Someti ...
- ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))
http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...
- 微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 Little Hi runs a web server. Sometimes he has to deny acces ...
- hihocoder #1290 : Demo Day (2016微软编程测试第三题)
#1290 : Demo Day 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics startup. ...
- CSDN挑战编程——《金色十月线上编程比赛第二题:解密》
金色十月线上编程比赛第二题:解密 题目详情: 小强是一名学生, 同一时候他也是一个黑客. 考试结束后不久.他吃惊的发现自己的高等数学科目竟然挂了,于是他果断入侵了学校教务部站点. 在入侵的过程中.他发 ...
- 【微软2017年预科生计划在线编程笔试第二场 B】Diligent Robots
[题目链接]:http://hihocoder.com/problemset/problem/1498 [题意] 一开始你有1个机器人; 你有n个工作; 每个工作都需要一个机器人花1小时完成; 然后每 ...
- 【微软2017年预科生计划在线编程笔试第二场 A】Queen Attack
[题目链接]:http://hihocoder.com/problemset/problem/1497 [题意] 给你n个皇后; 然后问你其中能够互相攻击到的皇后的对数; 皇后的攻击可以穿透; [题解 ...
- Queen Attack -- 微软2017年预科生计划在线编程笔试第二场
#!/usr/bin/env python # coding:utf-8 # Queen Attack # https://hihocoder.com/problemset/problem/1497 ...
- Parentheses Sequence微软编程笔试
描述 You are given a sequence S of parentheses. You are asked to insert into S as few parentheses as p ...
随机推荐
- Dubbo在Spring和Spring Boot中的使用
一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...
- 九度OJ 1510 替换空格
题目地址:http://ac.jobdu.com/problem.php?pid=1510 题目描述: 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We ...
- TreeSet集合
TreeSet集合 TreeSet集合是一个依靠TreeMap实现的有序集合,内部存储元素是自动按照自然排序进行排列,所以如果想要保留存储时的顺序,那么就不建议使用TreeSet. TreeSet继承 ...
- away3d打包到IOS锯齿问题解决办法
很多ios设备是高清屏,一个像素顶普通设备四个像素.从这点上也许可以入手解决. 先把<requestedDisplayResolution>high</requestedDispla ...
- thinkphp分页实现
以上为我对于thinkphp分页的实现效果,两种方法,一种调用公共函数中的函数方法(参考http://www.cnblogs.com/tianguook/p/4326613.html),一种是在模型中 ...
- MySQL查看和修改字符编码
MySQL的默认编码是Latin1,不支持中文,要支持中午需要把数据库的默认编码修改为gbk或者utf8. 1.需要以root用户身份登陆才可以查看数据库编码方式(以root用户身份登陆的命令为:&g ...
- 【BZOJ】1015: [JSOI2008]星球大战starwar
1015: [JSOI2008]星球大战starwar 题意:一个点数为N(1<= 40w),边数为M(1<=20w)的图,总共删除k个节点,问开始以及每次删除一个节点之后图的连通块数? ...
- ASP.NET MVC got 405 error on HTTP DELETE request
使用Backload的时候在本地调试通过,上传服务器后出现405错误(监控通信时可以发现ajax的返回结果为405) 通过修改webconfig可以解决: <system.webServer&g ...
- 转载 -- iOS数据持久化存储
作者:@翁呀伟呀 授权本站转载 概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方 ...
- iOS+JSPatch在线修改app功能-b
什么是热更新? 举个例子,你的app上架了,但是突然想添加个小功能,那么你有两种方法 第一种方法:在原生代码中修改源代码,然后提交到appStore,这个过程真是很漫长...虽然最近我提交的都是一两天 ...