IP Addresses
Time limit1sMemory limit1024 MB
Given a sequence of IP addresses added one by one, report which distinct addresses grep wrongly skipped because dots act as regex wildcards.
- Level
Medium6 of 10
- Topics
- String, Hash map, Implementation, Brute force
- Solved
- No attempts yet
Problem
Mati has a website and wants to know exactly who visits it. To track visitors, Mati wrote a script that works as follows:
- the IP addresses of all visitors seen so far are kept in a single text file;
- whenever a new request (visitor) arrives, the program
grepis used to check whether that IP address occurs in the text file (grep <newIP> <file>); - if no match is found, the new address is appended to the end of the file and a notification is sent to Mati;
- otherwise, nothing is done.
An IP address is a string made of four integers, each in the range , separated by dots (.).
grep is a common tool for searching text files for patterns described by regular expressions. Here Mati uses grep incorrectly, because:
grepsearches for substrings: the pattern need not start at the beginning of a line nor end at the end of a line;- the pattern is interpreted as a regular expression, so a dot (
.) in the searched IP address can match any character in the text file (but not the other way around: a digit in the pattern does not match a dot in the file).
You are given the list of all IP addresses processed by Mati's script, in processing order. Determine which addresses Mati's script erroneously failed to add to the file.
Input
The first line contains one integer, the number of IP addresses (). Each of the next lines contains one IP address. The input may contain repeated addresses; in any test, the total number of distinct addresses does not exceed .
Output
On the first line, print the number of distinct addresses that were not added. On each of the next lines, print one of the not-added addresses, in the order of their first appearance in the input.