您的位置:首页 > 其它

微软 403 Forbidden

2016-04-10 12:23 405 查看
/描述

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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  微软