问题描述:
样例输入
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;
}
}
}
}