微软 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

/**
 * 将IP地址转换为二进制,构架Trie树
 * Created by ustc-lezg on 16/4/10.
 */
public class Main {

    public static void main(String[] args) {
        solve();
    }

    private static void solve() {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            Trie trie = new Trie();
            for (int i = 0; i < n; i++) {
                String action = scanner.next();
                String ip = scanner.next();
                if (action.equals("allow")) {
                    trie.insert(ip, (byte) 1);
                } else {
                    trie.insert(ip, (byte) 2);
                }
            }
            for (int i = 0; i < m; i++) {
                String ip = scanner.next();
                if (trie.isAllow(ip)) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
            }
        }
        scanner.close();
    }

    private static String toBinary(String ip) {
        String[] ip_nums = ip.split("\\.");
        StringBuilder sb = new StringBuilder(33);
        for (String num : ip_nums) {
            String binary = Integer.toBinaryString(Integer.parseInt(num));
            int len = 8 - binary.length();//补全8位
            for (int i = 0; i < len; i++) {
                sb.append("0");
            }
            sb.append(binary);
        }
        return sb.toString();
    }

    //字典树
    private static class Trie {

        //节点
        class Node {
            byte flag;//flag=1,allow;flag=2,deny
            int seq;//规则顺序
            Node[] next = new Node[2];

            Node(byte flag) {
                this.flag = flag;
            }
        }

        private int seq;
        private Node root = new Node((byte) 0);

        private void insert(String ip, byte flag) {
            seq++;
            int mask = 32;
            int index = ip.indexOf('/');
            if (index != -1) {
                mask = Integer.parseInt(ip.substring(index + 1));
            } else {
                index = ip.length();
            }
            String ipBinary = toBinary(ip.substring(0, index));
            char[] ipBinarys = ipBinary.toCharArray();
            Node node = root;
            for (int i = 0; i < mask; i++) {
                //在规则i的前缀路径上存在规则r,直接丢弃规则i
                if (node.seq > 0) {
                    return;
                }
                int x = ipBinarys[i] - '0';
                if (node.next[x] == null) {
                    node.next[x] = new Node((byte) 0);
                }
                node = node.next[x];
            }
            //后面的规则不能覆盖前面的规则
            if (node.flag == 0) {
                node.flag = flag;
            }
            node.seq = seq;
        }

        private boolean isAllow(String ip) {
            String ipBinary = toBinary(ip);
            char[] ipBinarys = ipBinary.toCharArray();
            int len = ipBinarys.length;
            Node node = root;
            int seq = Integer.MAX_VALUE;
            boolean isallow = true;
            int index = 0;
            int i = 0;
            while (node != null) {
                if (i < len) {
                    index = ipBinarys[i] - '0';
                }
                if (node.flag == 1) {
                    if (node.seq < seq) {
                        isallow = true;
                        seq = node.seq;
                    }
                } else if (node.flag == 2) {
                    if (node.seq < seq) {
                        isallow = false;
                        seq = node.seq;
                    }
                }
                node = node.next[index];
                ++i;
            }
            return isallow;
        }
    }
}
import java.util.Scanner;

/**
 * 将IP地址转换为二进制,构架Trie树
 * Created by ustc-lezg on 16/4/10.
 */
public class Main {

    public static void main(String[] args) {
        solve();
    }

    private static void solve() {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            Trie trie = new Trie();
            for (int i = 1; i <= n; i++) {
                String action = scanner.next();
                String ip = scanner.next();
                if (action.equals("allow")) {
                    trie.insert(ip, i);
                } else {
                    trie.insert(ip, -i);
                }
            }
            for (int i = 0; i < m; i++) {
                String ip = scanner.next();
                if (trie.isAllow(ip)) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
            }
        }
        scanner.close();
    }

    private static String toBinary(String ip) {
        String[] ip_nums = ip.split("\\.");
        StringBuilder sb = new StringBuilder(33);
        for (String num : ip_nums) {
            String binary = Integer.toBinaryString(Integer.parseInt(num));
            int len = 8 - binary.length();//补全8位
            for (int i = 0; i < len; i++) {
                sb.append("0");
            }
            sb.append(binary);
        }
        return sb.toString();
    }

    //字典树
    private static class Trie {

        //节点
        class Node {
            int seq = 0;//规则顺序
            Node[] next = new Node[2];
        }

        private Node root = new Node();

        private void insert(String ip, int seq) {
            int mask = 32;
            int index = ip.indexOf('/');
            if (index != -1) {
                mask = Integer.parseInt(ip.substring(index + 1));
            } else {
                index = ip.length();
            }
            String ipBinary = toBinary(ip.substring(0, index));
            char[] ipBinarys = ipBinary.toCharArray();
            Node node = root;
            for (int i = 0; i < mask; i++) {
                //在规则i的前缀路径上存在规则r,直接丢弃规则i
                if (node.seq != 0) {
                    return;
                }
                int x = ipBinarys[i] - '0';
                if (node.next[x] == null) {
                    node.next[x] = new Node();
                }
                node = node.next[x];
            }
            if (node.seq != 0) {
                node.seq = seq;
            }
        }

        private boolean isAllow(String ip) {
            String ipBinary = toBinary(ip);
            char[] ipBinarys = ipBinary.toCharArray();
            Node node = root;
            int seq = 1;
            int index;
            for (int i = 0; i < 32; i++) {
                if (node.seq != 0) {
                    seq = node.seq;
                }
                index = ipBinarys[i] - '0';
                if (node.next[index] == null) {
                    break;
                }
                node = node.next[index];
            }
            return seq > 0;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值