CCF 2018年12月 第三题 CIDR合并



import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Main{
	public static List<Ip> list = new ArrayList();
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine();
		list.add(convertToBinaryIp(scanner.nextLine()));
		n--;
		boolean flag = false;
		while(n-->0){
			Ip ip = convertToBinaryIp(scanner.nextLine());
			for(int i = 0 ; i < list.size(); i++){
				if(ip.ip.compareTo(list.get(i).ip) == -1 || (ip.ip.compareTo(list.get(i).ip) == 0 && ip.length <list.get(i).length )){
					flag = true;
					list.add(i,ip);
					break;
				}
			}
			if(!flag){
				list.add(list.size(),ip);
			}
		}
		
		
		for(int i= 0; i < list.size()-1;i++){
			int j= i+1;
			while(true){
				if(isContain(list.get(i),list.get(j))){
					list.remove(j);
				}
				else {
					break;
				}
			}
		}
		
		for(int i = 0; i< list.size()-1;i++){
			int j = i+1;
			if(j>=list.size()){
				break;
			}
			if(isUnion(list.get(i), list.get(j))){
				list.get(i).length--;
				list.remove(j);
				if(i==0){
					i--;
				}
				else {
					i = i-2;
				}
			}
		}
		
		
		for(int i = 0; i < list.size();i++){
			char[] ip = list.get(i).ip.toCharArray();
			int tempIp[] = new int[5];
			int j = 0;
			int sum = 0;
			while(j<ip.length){			
				if(ip[j] == '1'){
					sum += Math.pow(2, 8-(j%8+1));
				}
				
				if((j+1)%8 == 0){
					tempIp[j/8] = sum;
					sum= 0;
				}
				j++;
			}
			tempIp[4] = list.get(i).length;
		
			System.out.printf(tempIp[0]+"."+tempIp[1]+"."+tempIp[2]+"."+tempIp[3]+"/"+tempIp[4]);
			System.out.println();
		
		}
		
		
		
		
	}
	
	public static boolean isContain(Ip a, Ip b) {
		if(a.length >= b.length){
			return false;
		}
		
		return a.ip.substring(0,a.length).equals(b.ip.substring(0, a.length));
		
	}
	
	public static boolean isUnion(Ip a, Ip b){
		if(a.length != b.length){
			return false;
		}
		

		return a.ip.substring(0, a.length-1).equals(b.ip.substring(0, b.length-1)) && a.ip.substring(a.length,a.length) != b.ip.substring(a.length,a.length);
		
	}
	
	public static Ip convertToBinaryIp( String input) {
		String result = new String();
		int length = 0;
		String temp[] = input.split("/")[0].split("\\.");
		for(int i = 0; i< temp.length; i++){
			int tempVal = Integer.parseInt(temp[i]);
			for(int j = 7; j>=0; j--){
				if(tempVal>=Math.pow(2, j)){
					result +="1";
					tempVal-=Math.pow(2, j);
				}
				else {
					result +="0";
				}
			}
		}
		if(input.split("/").length == 2){
			length = Integer.parseInt(input.split("/")[1]);
		}
		else {
			length = 8* temp.length;
		}
		
		for(int i = temp.length; i < 4; i++){
			result +="00000000";
		}
		
		return new Ip(result, length);
	}
	
	
	public static class Ip{
		public String ip;
		public int length;
		
		public Ip(String ip, int length){
			this.ip = ip;
			this.length = length;
		}
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值