zoukankan      html  css  js  c++  java
  • UVA 1590 IP Networks

    Description

    Download as PDF

    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 0$ le$n$ le$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 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(1$ le$m$ le$1000) . 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
    
    
    
    
    思路:
    ip的四个部分分别处理,每一部分找出最大值的最小值,然后判断是其二进制最后几位不同,得出子网掩码;
    用任意一个IP与子网掩码进行按位与运算得出最小IP;
    
    
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    int zwym_table[9] = {255, 254, 252, 248, 240, 224, 192, 128, 0};
    
    int main()
    {
    	int ip[4][1024];
    	int m;
    	while(scanf("%d",&m)!=EOF)
    	{
    		memset(ip, 0, sizeof(ip));
    		int zwym[4];
    		int minip[4];
    		for(int i=0; i<m; i++)
    			scanf("%d.%d.%d.%d",&ip[0][i], &ip[1][i], &ip[2][i], &ip[3][i]);
    		for(int i=0; i<4; i++)
    		{
    			int dif=0, x, j;
    			int p,q;
    			sort(ip[i], ip[i]+m);
    			p = ip[i][m-1];
    			q = ip[i][0];
    			for(j=1; j<=8; j++)
                {
                    if(p%2!=q%2)
                        dif = j;
                    p = p/2;
                    q = q/2;
                }
    			zwym[i] = zwym_table[dif];
    			minip[i] = ip[i][0] & zwym[i];
    		}
    		for(int i=0; i<4; i++)
    		{
    			if(zwym[i] != 255)
    			{
    				for(i = i+1; i<4; i++)
    				{
    					zwym[i] = 0;
    					minip[i] = 0;
    				}
    				break;
    			}
    		}
    		printf("%d.%d.%d.%d
    ",minip[0], minip[1], minip[2], minip[3]);
    		printf("%d.%d.%d.%d
    ",zwym[0], zwym[1], zwym[2], zwym[3]);
    	}
    	return 0;
    }


  • 相关阅读:
    【bzoj2821】作诗(Poetize)
    ZOJ-2112-Dynamic Rankings(线段树套splay树)
    POJ- 2104 hdu 2665 (区间第k小 可持久化线段树)
    hust-1024-dance party(最大流--枚举,可行流判断)
    hdu-3046-Pleasant sheep and big big wolf(最大流最小割)
    POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)
    POJ-Common Substrings(后缀数组-长度不小于 k 的公共子串的个数)
    POJ-2774-Long Long Message(后缀数组-最长公共子串)
    POJ-3693-Maximum repetition substring(后缀数组-重复次数最多的连续重复子串)
    spoj-694-Distinct Substrings(后缀数组)
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312802.html
Copyright © 2011-2022 走看看