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 ≤ 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 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 ≤ m ≤ 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地址中的最高位开始枚举最长的公共子串,此时32-该串长度为题目中的n,之后按照要求输出即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
/************************************************** filename :o.cpp author :maksyuki created time :2017/12/9 9:03:35 last modified :2017/12/9 10:01:00 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; char si[66], s[1000+6][36]; int tmp[6], m; void convert(int p) { int len = 1; int tmpv = 0; for(int i = 0; si[i]; i++) { if(si[i] == '.') { tmp[len++] = tmpv; tmpv = 0; } else tmpv = tmpv * 10 + (si[i] - '0'); } tmp[len++] = tmpv; char ans[66]; len = 0; bool is_first = true; for(int i = 1; i <= 4; i++) { int vv = tmp[i]; while(vv) { ans[len++] = char(vv % 2 + '0'); vv /= 2; } while(len <= 7) ans[len++] = '0'; ans[len] = 0; //if(i > 1) strcat(s[p] + 1, "."); for(int i = 0; i < len / 2; i++) swap(ans[i], ans[len-1-i]); if(is_first) { is_first = false; strcpy(s[p] + 1, ans); } else strcat(s[p] + 1, ans); len = 0; } } char res[6][66]; int solve() { for(int i = 1; i <= 32; i++) { bool is_same = true; for(int j = 2; j <= m; j++) if(s[j][i] != s[1][i]) { is_same = false; break; } if(!is_same) return i - 1; res[1][i] = s[1][i]; } return 32; } void rconvert(int p) { for(int i = 8; i <= 32; i += 8) { int tmp = 0, bb = 1; for(int j = i; j >= i - 7; j--) { tmp += bb * (res[p][j] - '0'); bb <<= 1; } if(i > 8) printf("."); printf("%d", tmp); tmp = 0; } puts(""); } int main() { #ifdef LOCAL CFF; //CFO; #endif while(scanf("%d", &m) != EOF) { for(int i = 1; i <= m; i++) { scanf(" %s", si); convert(i); } int vv = solve(); for(int i = vv + 1; i <= 32; i++) res[1][i] = '0'; res[1][33] = 0; for(int i = 1; i <= vv; i++) res[2][i] = '1'; for(int i = vv + 1; i <= 32; i++) res[2][i] = '0'; rconvert(1); rconvert(2); } return 0; } |
- « 上一篇:uva253
- uva508:下一篇 »