Cafe Bazaar IP Database
Time limit2sMemory limit512 MB
Given up to 100 IPv5 address ranges in CIDR or first-last form, merge them and output the minimum number of disjoint CIDR blocks covering exactly the same addresses.
- Level
Medium7 of 10
- Topics
- Implementation, Greedy, Sorting, Bit manipulation
- Solved
- No attempts yet
Problem
Cafe Bazaar has a heavy load on its servers, so it keeps a daily log of the traffic to them for performance optimization and statistics. The IP addresses in each day's log are stored in a database in one of the two formats explained below, the IP range format or the CIDR format.
The IP database software uses a new version of the Internet Protocol (IP) called IPv5. IPv5 provides more IP addresses by extending the number of address bits from 32 in IPv4 to 40. Each IP address in IPv5 is a 40 bit numerical label assigned to a device connected to the Internet. Each address is written as a sequence of 5 numbers, called bytes, each with a decimal value from 0 to 255. IP addresses are usually written in dot decimal notation, for example "134.172.16.254.1". The notation prints the five bytes of the address in decimal, separated by periods. Leading zero bytes are not removed in this notation. For example, the correct representation of the address with numerical value 0 is "0.0.0.0.0".
Classless Inter-Domain Routing (CIDR) is a way of specifying a range of IP addresses. A CIDR looks like a normal IP address except that it ends with a slash followed by a number. The CIDR , in which is an IP address and is an integer from 0 to 40 inclusive, denotes the range of addresses whose leftmost bits equal the leftmost bits of . The remaining bits are free to be either 0 or 1. To give each range a single representation, the rightmost bits of must all be 0.
The IP range format is another way of specifying a range of IP addresses. A range in this format is written as its first and its last address in dot decimal notation, joined by a dash (-). The first address is not larger than the last address when IP addresses are read as 5 digit numbers in base 256.
For example, "128.192.168.200.0/32" is a CIDR in which the first 32 bits (4 bytes, "128.192.168.200") are the same for every address in the range and only the last byte differs. The same range in the IP range format is "128.192.168.200.0-128.192.168.200.255".
The IP database keeps growing, because customers come from all around the world and the service has run for many years. Cafe Bazaar therefore plans to reduce the number of entries by representing the stored ranges with the minimum number of CIDRs. The new CIDRs must not include any IP address that does not belong to the IP database. Exactly one set of CIDRs satisfies both conditions.
Input
The input holds several test cases. For each test case, the first line contains an integer , the number of IP ranges in the IP database (). Each of the next lines contains one IP range, either in the CIDR format or in the IP range format. The input ends with a line containing 0, which is not processed.
Output
For each test case, print the minimum number of CIDRs that represent the whole IP database on one line, then print those CIDRs in increasing order of IP value, one per line.