[Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)

传送门

#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的每一位数字

1289403 ForbiddenACG++970ms53MB2分钟前查看

 

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <algorithm>
  5 #include <string>
  6 #include <cstring>
  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值