There is no place like 127.0.0.1
Time limit2sMemory limit256 MB
Replace each IPv4 address in the text with its mapped word from single entries and non-overlapping ranges, leaving unmatched addresses unchanged.
- Level
Easy3 of 10
- Topics
- Binary search, Sorting, Hash map
- Solved
- No attempts yet
Problem
A friend of yours replaces common English words with IPv4 addresses in his documents. You want to turn those addresses back into the original words. You got hold of his table of words and addresses. Some entries in the table give a range of addresses instead of a single one.
An IPv4 address is a 32 bit unsigned integer, usually written in dot decimal notation: four 8 bit unsigned integers joined by dots, for example 172.30.12.255. When the address is read as an integer, the first number is the most significant byte.
Replace every address the table covers with the word from the table, and leave every other address alone.
Input
The first line contains an integer (), the number of test cases.
Each test case has a map section and a text section.
The map section starts with an integer () on its own line, the number of map entries. Each of the next lines holds one entry: a word followed by one IPv4 address, or a word followed by two IPv4 addresses. In the first form only that single address is replaced with the word. In the second form every address inside the range is replaced with the word. When a range is given, the first address is always smaller than the second one.
The map has no duplicate entries. No word appears twice, no address appears twice, and no two ranges overlap, so at most one entry covers any given address.
The text section starts with an integer () on its own line, the number of lines of text. Each of the next lines holds at most 20 words and IPv4 addresses separated by single spaces. A word contains only letters of the English alphabet. An address contains only digits and dots, every octet is between 0 and 255, and octets are written without leading zeros.
Output
For each test case, process the lines of text in order. Print each line with every address the map covers replaced by its word. Print an address that has no matching word exactly as it appeared, and print words unchanged. Separate the items on a line with single spaces.