问题描述
- 微软4月笔试题第二题,为什么本地运行没错,提交是RE,实在想不出来,求救!!
-
import java.util.ArrayList; import java.util.Scanner; public class Main { int allowS = 0;//rules allow数组大小 int denyS = 0; ArrayList<String> allow = new ArrayList<>();//用来存放动态变化的rules,整个类都要使用,则定义为实例变量 ArrayList<String> deny = new ArrayList<>(); public static void main(String[] args) { Scanner input = new Scanner(System.in); int N = input.nextInt(); int M = input.nextInt(); Main webser = new Main(); if(1<=N && M<=100000){ webser.rules(N); String[] output = webser.request(M); for(int j=0;j<output.length;j++){ System.out.println(output[j]); } //sca.close();//写完之后就要close,否则容易忘记 } input.close(); } /* * @param n为rules个数,此方法用来封装rules */ public void rules(int n){ @SuppressWarnings("resource") Scanner sca = new Scanner(System.in); for(int i=0;i<n;i++){ String s = sca.nextLine(); String[] rule = s.split(" ");//返回字符串数组 if(rule[0].equals("allow")){ allow.add(rule[1]); allowS++; }else if(rule[0].equals("deny")){ deny.add(rule[1]); denyS++; } } //sca.close();如果把这个流关闭了,再调用request方法中的输入流就不行了 } public String[] request(int m){ //开始匹配,最优的应该是用正则表达式匹配,稍后优化 String[] output1 = new String[m];//没问题 String[] requ = new String[m];//创建请求数组 @SuppressWarnings("resource") Scanner sca = new Scanner(System.in); for(int i=0;i<m;i++){ requ[i] = sca.nextLine();//输入请求ip数组 } //开始遍历每一次request是否符合命中rules circle: for(int j=0;j<m;j++){ for(int k=0;k<allow.size();k++){ if(requ[j].equals(allow.get(k))){ output1[j] = "YES";//return "YES"; continue circle; }else if(allow.get(k).contains("/")){ String requBinary =IPtoInt.ipToint(requ[j]); String[] allowip = allow.get(k).split("/"); int mask =Integer.parseInt(allowip[1]);//取位数 String allowstr = IPtoInt.ipToint(allowip[0]); //String allowMask = allowstr.substring(0,mask);//变成字符串后,取前几位 if(requBinary.substring(0, mask).equals(allowstr.substring(0,mask))){ output1[j] = "YES";//return "YES"; continue circle; } } } for(int k=0;k<deny.size();k++){ if(requ[j].equals(deny.get(k))){ output1[j] = "NO";//return "NO"; continue circle; }else if(deny.get(k).contains("/")){ String requBinary = IPtoInt.ipToint(requ[j]); String[] denyip = deny.get(k).split("/"); int mask = Integer.parseInt(denyip[1]); String denystr = IPtoInt.ipToint(denyip[0]); String denyMask = denystr.substring(0,mask); if((requBinary.substring(0,mask)).equals(denyMask)){//必须对比前mask位 output1[j] = "NO";//return "NO"; continue circle; } } } if(output1[j].length()==0) output1[j] = "YES"; } //sca.close(); return output1;//返回字符串数组 } } class IPtoInt { // 重写了方法,将ip地址转换为二进制。 public static String ipToint(String strIP) { String[] ip = new String[4]; String zero = "00000000";//用来补位的 String[] ipstr = strIP.split("\."); // 将每个.之间的字符串转换成整型 ip[0] = Integer.toBinaryString(Integer.parseInt(ipstr[0])); ip[1] = Integer.toBinaryString(Integer.parseInt(ipstr[1])); ip[2] = Integer.toBinaryString(Integer.parseInt(ipstr[2])); ip[3] = Integer.toBinaryString(Integer.parseInt(ipstr[3])); for(int i=0;i<4;i++){ if(ip[i].length()<8){ ip[i] =zero.substring(0,8-ip[i].length()) + ip[i];//如果每一段ip不足八位,那么用字符串加法高位补0,不用循环,一次性补0,因为ip.length()一直会变 } } return ip[0]+ip[1]+ip[2]+ip[3]; } }
解决方案
http://blog.csdn.net/u112222222/article/details/51164396
时间: 2024-09-22 21:47:30