hibocoder 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 with10000000011111110000100001111101 (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.

其实就是对ip的处理,我在做的时候,由于对java的split函数使用较少,忘记特殊符号要做“\\”的注释处理,花了很久,看来真的是“绝知此事要躬行”。

这里有一个小bug花了好久才看到,留给大家来检查

import java.util.*;

/**
 * Created by Administrator on 2016/4/6.
 */
public class Main{
    public static void main(String[] args){
        int N,M;
        Scanner scaner = new Scanner(System.in);
        N = scaner.nextInt();
        M = scaner.nextInt();
        String[] ta=new String[N];
        String[] tad=new String[N];
        String[] ad=new String[M];
        int[] flag = new int[N];
        int[] fl = new int[N];
        for(int i = 0;i<N;i++){
            flag[i]=32;
            ta[i] = scaner.nextLine();
            tad[i] = scaner.nextLine();
            int l = tad[i].length();
            for(int j=0;j<l;j++){
                if(tad[i].charAt(j)=='/'){
                   flag[i]=0;
                    break;
                }

            }
            if(flag[i]==32) {
              String ao[]=tad[i].split(".");
                String s = ao[0]+ao[1]+ao[2]+ao[3];
                    fl[N]= Integer.parseInt(s);

            }else{
                String ao[] = tad[i].split(".");
                String df[] = ao[3].split("/");
                String s = ao[0]+ao[1]+ao[2]+df[0];
                fl[i] = Integer.parseInt(s);
                flag[i] = Integer.parseInt(df[1]);
            }
            fl[i] = fl[i] >> (32-flag[i]);
        }
        int[] fg = new int[M];
        for(int i = 0;i<M;i++){
            ad[i] = scaner.nextLine();
            String ao[]=ad[i].split(".");
            String s = ao[0]+ao[1]+ao[2]+ao[3];
            fg[i]= Integer.parseInt(s);
            fg[i] = fg[i] >> (32-flag[i]);
        }
        for(int i = 0;i<M;i++){
            for(int j = 0;j<N;j++){
                if(fg[i]==fl[i]){
                    if(ta[i]=="allow"){
                        System.out.println("YES");
                        break;
                    }else{
                        System.out.println("NO");
                        break;
                    }
                }
            }
            
        }
    }
}

 

posted on 2016-04-12 15:20  没结果的花  阅读(181)  评论(0编辑  收藏  举报

导航