UVA - 1590 IP Networks

Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he decided to 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 from 0 to 255 (inclusive) without extra leading zeroes.

IP network is described by two 4-byte numbers - network address and network mask. Both network address 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 0n32 . 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 last bits. 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).

Input

The input file will contain several test cases, each of them as described below.

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

Output

For each test case, write to the output file two lines that describe the smallest possible IP network that contains all IP addresses from the input file. Write network address on the first line and network mask on the second line.

Sample Input

3 
194.85.160.177 
194.85.160.183 
194.85.160.178

Sample Output

194.85.160.176 
255.255.255.248

Tips

 1. 将n个IP地址转化为二进制,如样例
 2. 11000010010101011010000010110000
	11000010010101011010000010110111
	11000010010101011010000010110010
 3.从右开始找到第一个不相同的字符的下标,最小网络就是前面相同后边为0
 	11000010010101011010000010110000
 	子关掩码就是前面为1,后面为0
 	11111111111111111111111111111000
 	最后转化为十进制即可
#include<bits/stdc++.h>
#define read() freopen("input.txt","r",stdin);
#define write() freopen("output.txt","w",stdout);
using namespace std;
struct net{
	int ip[4];
	string str;
};
void change(net &a){
	for( int i=0; i<4; i++ ){
		string ss(8,'0');
		int d=7,n=a.ip[i];
		while(n!=0){
			ss[d--]=n%2+'0';n/=2;
		}
		a.str+=ss;
	}
}
int change1(string str){
	int num=0;
	for( int i=0; i<8; i++ ){
		num+=(str[i]-'0')*pow(2,8-i-1);
	}
	return num;
}
int main() {
	read();write();
	int n;
	while(cin>>n){
		net s1[n];int d=0;
		string ans1(32,'1'),ans2(32,'0');
		for( int i=0; i<n; i++ ){
			scanf("%d.%d.%d.%d",&s1[i].ip[0],&s1[i].ip[1],&s1[i].ip[2],&s1[i].ip[3]);
			change(s1[i]);
		}
		for( d=0; d<32; d++ ){
			int cnt=0;
			for( int j=0; j<n; j++ ) if(s1[j].str[d]=='1') cnt++;
			if(cnt==n||cnt==0) continue;
			else break;
		}
		for( int i=0; i<d; i++ ) ans2[i]=s1[0].str[i];
		for( int i=d; i<32; i++ ){ ans1[i]='0';ans2[i]='0';}
		for( int i=0; i<32; i+=8 ){
			if(i) cout<<'.';
			string buf=ans2.substr(i,8);cout<<change1(buf);
		}
		cout<<'\n';
		for( int i=0; i<32; i+=8 ){
			if(i) cout<<'.';
			string buf=ans1.substr(i,8);cout<<change1(buf);
		}
		cout<<'\n';
	}		
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43323172/article/details/89791297
今日推荐