201812-3-CIDR合并-运行超时(未解决)

问题描述:
在这里插入图片描述

样例输入
2
1
2
样例输出
1.0.0.0/8
2.0.0.0/8
样例输入
2
10/9
10.128/9
样例输出
10.0.0.0/8
样例输入
2
0/1
128/1
样例输出
0.0.0.0/0

import java.util.*;
public class Main {	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();	
		ArrayList<Ip> list=new ArrayList<Ip>(n);
		for(int i=0;i<n;i++) {
			list.add(new Ip(in.next()));
		}
		Collections.sort(list);
		for(int i=0;i<list.size()-1;i++) {
			if(list.get(i).compareTo(list.get(i+1))==0) {
				list.remove(i);
				i--;
			}
		}
		for(int i=0;i<list.size()-1;i++) {
			if(list.get(i).matches(list.get(i+1))) {
				list.remove(i+1);
				i--;
			}
		}
		Ip newIp;
		for(int i=0;i<list.size()-1;i++) {
			newIp=list.get(i).merge(list.get(i+1));
			if(newIp!=null) {	
				list.set(i, newIp);
				list.remove(i+1);
				i--;
				while(i>=0) {
					newIp=list.get(i).merge(list.get(i+1));			
					if(newIp!=null) {
						list.set(i, newIp);
						list.remove(i+1);
						i--;
					}else {
						break;
					}
				}
			}	
		}
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));
		}
	}
	
}
class Ip implements Comparable{
	byte[]ip=new byte[5];
	public Ip() {}
	public Ip(String str) {
		if(str.contains("/")) {
			String temp[]=str.split("/");
			ip[4]=(byte)Integer.parseInt(temp[1]);
			temp=temp[0].split("\\.");
			for(int i=0;i<4;i++) {
				if(i<temp.length) {
					ip[i]=(byte)Integer.parseInt(temp[i]);
				}else {
					ip[i]=0;
				}
			}
		}else {
			String []temp=str.split("\\.");
			ip[4]=(byte)(temp.length*8);
			for(int i=0;i<4;i++) {
				if(i<temp.length) {
					ip[i]=(byte)Integer.parseInt(temp[i]);
				}else {
					ip[i]=0;
				}
			}
		}
	}
	public String toString() {
		String ip="";
		if(this.ip[0]<0)
			ip+=this.ip[0]+256;
		else
			ip+=this.ip[0];
		for(int i=1;i<4;i++) {
			ip+=".";
			if(this.ip[i]<0)
				ip+=this.ip[i]+256;
			else
				ip+=this.ip[i];
		}
		return ip+="/"+this.ip[4];
	}
	public int compareTo(Object o) {
		Ip anotherIp=(Ip)o;
		int address[][]=new int[2][5];
		for(int i=0;i<4;i++) {
			if(this.ip[i]<0)
				address[0][i]=this.ip[i]+256;
			else
				address[0][i]=this.ip[i];
			if(anotherIp.ip[i]<0)
				address[1][i]=anotherIp.ip[i]+256;
			else
				address[1][i]=anotherIp.ip[i];
		}
		address[0][4]=this.ip[4];
		address[1][4]=anotherIp.ip[4];
		for(int i=0;i<5;i++) {
			if(address[0][i]<address[1][i]) {
				return -1;
			}else if(address[0][i]>address[1][i]) {
				return 1;
			}
		}
		return 0;	
	}
	public boolean matches(Ip anotherIp) {
		if(this.ip[4]<anotherIp.ip[4]) {
			for(int i=0;i<this.ip[4]/8;i++) {
				if(this.ip[i]!=anotherIp.ip[i]) {
					return false;
				}
			}
			byte temp=-1;
			if(this.ip[4]%8!=0) {
				if(this.ip[this.ip[4]/8]==((anotherIp.ip[this.ip[4]/8])&(byte)(temp<<((byte)(8-this.ip[4]%8))))){
					return true;
				}else {
					return false;
				}
			}else {
				return true;
			}
		}else {
			return false;
		}
	}
	public Ip merge(Ip anotherIp) {
		if(this.ip[4]!=anotherIp.ip[4])
			return null;
		else {
			int length=this.ip[4]-1;
			for(int i=0;i<length/8;i++) {
				if(this.ip[i]!=anotherIp.ip[i])
					return null;
			}
			byte temp=-1;
			Ip newIp;
			if(length%8!=0) {
				byte temp1=(byte) (this.ip[length/8]&(byte)(temp<<((byte)(8-length%8))));
				if(temp1==((anotherIp.ip[length/8])&(byte)(temp<<((byte)(8-length%8))))){
					newIp=new Ip();
					for(int i=0;i<4;i++) {
						if(i<length/8)
							newIp.ip[i]=this.ip[i];
						else if(i==length/8) {
							newIp.ip[i]=temp1;
						}else
							newIp.ip[i]=0;
					}
					newIp.ip[4]=(byte)length;
					return newIp;
				}else {
					return null;
				}
			}else {
				newIp=new Ip();
				for(int i=0;i<4;i++) {
					if(i<length/8)
						newIp.ip[i]=this.ip[i];
					else
						newIp.ip[i]=0;
				}
				newIp.ip[4]=(byte)length;
				return newIp;
			}
			
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值