首页 > 专题系列 > Java解POJ > POJ 2799 IP Networks [解题报告] Java
2013
11-12

POJ 2799 IP Networks [解题报告] Java

IP Networks

问题描述 :

Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he decidedto group all those IP addresses into the smallest possible IP network.

Each IP address is a 4-byte number that is written byte-by-byte in a decimal dot-separated notation “byte0.byte1.byte2.byte3″ (quotes are added for clarity). Each byte is written as a decimal number from0 to 255 (inclusive) without extra leading zeroes.

IP network is described by two 4-byte numbers – network address and network mask. Both networkaddress and network mask are written in the same notation as IP addresses.

In order to understand the meaning of network address and network mask you have to consider their binary representation. Binary representation of IP address, network address, and network mask consists of 32 bits: 8 bits for byte0 (most significant to least significant), followed by 8 bits for byte1, followed by 8 bits for byte2, and followed by 8 bits for byte3.

IP network contains a range of 2n IP addresses where 0 <= n <= 32. Network mask always has 32-n first bits set to one, and n last bits set to zero in its binary representation. Network address has arbitrary 32 - n first bits, and n last bits set to zero in its binary representation. IP network contains all IP addresses whose 32-n first bits are equal to 32-n first bits of network address with arbitrary n lastbits. We say that one IP network is smaller than the other IP network if it contains fewer IP addresses.

For example, IP network with network address 194.85.160.176 and network mask 255.255.255.248 contains 8 IP addresses from 194.85.160.176 to 194.85.160.183 (inclusive).

输入:

The first line of the input contains a single integer number m (1 <= m <= 1000). The following m lines contain IP addresses, one address on a line. Each IP address may appear more than once in a case.

输出:

Write to the output two lines that describe the smallest possible IP network that contains all IP addresses from this case. Write network address on the first line and network mask on the secondline.

样例输入:

3
194.85.160.177
194.85.160.183
194.85.160.178

样例输出:

194.85.160.176
255.255.255.248

解题代码:

//* @author: SmilingWang
import java.util.*;
import java.io.*;

public class Main {
 public static void main(String[] args) throws Exception{
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));	
   int n = Integer.parseInt(br.readLine());
   IPAddress[] ips = new IPAddress[n];
   for(int i = 0; i < n; i++){
	ips[i] = new IPAddress();
	ips[i].read(br.readLine());
   }
  int minlen = -1;
  for(int i = 1; i < n; i++){
    for(int j = 31; j >= 0; j--){
	if(ips[i].bitSet.get(j) != ips[0].bitSet.get(j)){
	   if(j > minlen){
		minlen = j;
	   }
	   break;
	}
    }
  }
  for(int i = minlen; i >= 0; i--){
	ips[0].bitSet.set(i, false);
  }
  System.out.println(ips[0].write());
  IPAddress mask = new IPAddress();
  for(int i = 31; i > minlen; i--){
	mask.bitSet.set(i, true);
   }
  System.out.println(mask.write());
 }
}

class IPAddress{
 BitSet bitSet = new BitSet(32);
 IPAddress(){		
 }

 public void read(String s){
   StringTokenizer st = new StringTokenizer(s, ".");
   LinkedList< Integer> stk = new LinkedList< Integer>();
	
   int k = 31;
   while(st.hasMoreTokens()){
    int t = Integer.parseInt(st.nextToken());
    int c = 0;
    while(t > 0){
	stk.addFirst((t%2));
	t /= 2;
	c++;
     }
     while(c < 8){
	stk.addFirst(0);
	c++;
     }
     while(!stk.isEmpty()){
	int v = stk.removeFirst();
	//q.addLast(v);
	bitSet.set(k, v == 1);
	k--;
      }
     }
   }

   public String write(){
     //System.out.println(bitSet);
     String strOut = "";
     for(int i = 31; i >= 0; i -= 8){
	int sum = 0;
	for(int j = i; j > i-8; j--){
		sum = sum * 2 + (bitSet.get(j) ? 1 : 0);
	}
	strOut += sum;
	if(i > 8){
		strOut += ".";
	}
     }
     return strOut;
   }
}