博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)
阅读量:6866 次
发布时间:2019-06-26

本文共 4321 字,大约阅读时间需要 14 分钟。

#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/30deny 1.1.1.1allow 127.0.0.1allow 123.234.12.23/3deny 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 5allow 1.2.3.4/30deny 1.1.1.1allow 127.0.0.1allow 123.234.12.23/3deny 0.0.0.0/01.2.3.41.2.3.51.1.1.1100.100.100.100219.142.53.100
样例输出
YESYESNOYESNO

题解转自:

以及

  田神的代码

 

题解: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分钟前

 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 8 int const N = 4e6; 9 #define ll long long 10 #define inf 0x3ffffff 11 12 using namespace std; 13 14 struct Trie 15 { 16 int root,tot; 17 int next[N][2]; //下一个节点编号 18 int end[N]; //0表示末尾为deny 19 int id[N]; //第几个ip 20 int NewNode() 21 { 22 memset(next[tot],-1,sizeof(next[tot])); 23 end[tot] = -1;id[tot] = -1; 24 return tot++; 25 } 26 void ini() 27 { 28 memset(id,-1,sizeof(id)); 29 tot = 0; 30 root = NewNode(); 31 } 32 void insert(ll x,int mask,int end_tmp,int id_tmp) //mask,匹配位数 33 { 34 int p = root; 35 ll cnt = 1LL << 31; 36 for(int i = 0;i < mask;i++){ 37 int digit; 38 if( (x & cnt) == cnt ) digit = 1; 39 else digit = 0; 40 if(next[p][digit] == -1){ 41 next[p][digit] = NewNode(); 42 } 43 p = next[p][digit]; 44 cnt>>=1; 45 } 46 if(id[p] == -1){ //要判断,防止被后面的 rule替换了 47 id[p] = id_tmp; 48 end[p] = end_tmp; 49 } 50 } 51 int search(ll x) 52 { 53 int p = root; 54 int res_id = inf; 55 int res_end = -1; 56 ll cnt = 1LL << 31; 57 if(id[p] != -1){ 58 res_id = id[p]; 59 res_end = end[p]; 60 } 61 for(int i = 0;i < 32;i++){ 62 int digit; 63 if( (x & cnt) == cnt ) digit = 1; 64 else digit = 0; 65 if(next[p][digit] == -1){ 66 break; 67 } 68 p = next[p][digit]; 69 if(id[p] != -1 && id[p] < res_id){ //要判断,选取最小的id 70 res_id = id[p]; 71 res_end = end[p]; 72 } 73 cnt>>=1; 74 } 75 return res_end; 76 } 77 }tr; 78 79 char s[10]; 80 81 int main() 82 { 83 int n,m; 84 //freopen("in.txt","r",stdin); 85 tr.ini(); 86 int a1, a2, a3, a4; 87 char ch; 88 ll x; 89 int mask; 90 scanf("%d %d", &n, &m); 91 int tmp_end; 92 for(int i = 1; i <= n; i++) 93 { 94 scanf("%s %d.%d.%d.%d", s, &a1, &a2, &a3, &a4); 95 mask = 32; 96 if(strcmp(s,"allow")==0) tmp_end = 1; 97 else tmp_end = 0; 98 scanf("%c", &ch); 99 int flag = (ch == '/');100 if(flag)101 scanf("%d", &mask);102 x = (a1 << 24) + (a2 << 16) + (a3 << 8) + a4;103 tr.insert(x,mask,tmp_end,i);104 }105 while(m --)106 {107 scanf("%d.%d.%d.%d", &a1, &a2, &a3, &a4);108 x = (a1 << 24) + (a2 << 16) + (a3 << 8) + a4;109 printf("%s\n", tr.search(x) ? "YES" : "NO");110 }111 112 }

 

转载于:https://www.cnblogs.com/njczy2010/p/5373660.html

你可能感兴趣的文章
异常分析法
查看>>
.NET平台和Csharp编程开发学习教程
查看>>
docker快速部署sql server 2017开发版
查看>>
Applicatin Loader 3.0 安装步骤
查看>>
XenDesktop 5 修改XML端口
查看>>
【 58沈剑 架构师之路】究竟啥才是互联网架构“高并发”
查看>>
ionic 自动生成APP图标与启动界面
查看>>
Sql存储过程
查看>>
dsquery命令查询总结
查看>>
利用openssl API进行简单网络编程
查看>>
过度加班? - 是该到了认真考虑的时候了。
查看>>
linux启动出问题
查看>>
linux查看哪些进程占用swap空间
查看>>
VS_断点无效
查看>>
关于“无敌删除命令”
查看>>
017 搭建eureka注册中心
查看>>
nis服务器搭建
查看>>
红帽企业存储管理之DRBD应用详解
查看>>
Linux下mail服务器架构之源码实现postfix邮件基本功能
查看>>
Bios加密
查看>>