Phone Numbers
Time limit2sMemory limit512 MB
Given country codes and operator codes, split each 11-digit number into its parts and print it in the phone book format, or Incorrect.
- Level
Medium5 of 10
- Topics
- String, Implementation, Brute force, Hash map
- Solved
- No attempts yet
Problem
A phone book is an essential part of any mobile phone's software. In the earliest mobile phones, it could store only subscriber names and their phone numbers. In more modern models, it can also store plenty of other useful information: a person's email address, their photograph, links to their social network pages, and their date of birth. Yet even when implementing the simplest version of this program, which works only with phone numbers, developers sometimes run into difficulties.
Assume that every phone number consists of 11 digits and is divided into three parts, each of which is a number without leading zeros. The first part consists of one, two, or three digits and is the country code where the phone is registered. The second part can consist of three, four, or five digits and can be either the region code where the number is registered or the mobile operator code that owns the number. The third part consists of all the remaining digits of the number and is the specific subscriber's number.
When a phone number is displayed on the phone screen, its parts are usually separated by various symbols so that the number is easier to read and remember. The country code is usually preceded by the <<+>> symbol, the region or operator code is placed in parentheses, and the subscriber number is divided by <<->> symbols into several parts. How many parts it is split into depends directly on the number of digits in it:
- if the subscriber number consists of three digits, it is a single part consisting of three digits;
- if the subscriber number consists of four digits, it is split into two parts, each consisting of two digits;
- if the subscriber number consists of five digits, it is split into two parts, the first consisting of three digits and the second of two;
- if the subscriber number consists of six digits, it is split into three parts, each consisting of two digits;
- if the subscriber number consists of seven digits, it is split into three parts, the first consisting of three digits and all the rest of two.
Naturally, a person entering a new number into their phone's phone book will not think about these rules and will simply enter it as a sequence of 11 digits. However, before displaying the number on the screen, the phone's software must determine which part of this number is the country code, which is the region or operator code, and which is the subscriber number, and format the number according to the rules described above. To make this task solvable, the phone's memory stores information about which country codes currently exist and which operator or region codes exist in those countries. You must implement a program that, given this information, correctly formats the numbers stored in the phone's phone book.
Input
The first line of the file contains a single integer (), the number of countries whose phone code information is stored in the phone's memory. This is followed by descriptions of these countries, separated by line breaks.
The first line of each country's description contains two integers and (, ), the phone code of this country and the number of operators or regions existing in this country. The next lines of this country's description contain integers, each at least 100 and at most 99999, the codes of operators or regions registered in this country.
The next line of the input file contains a single integer (), the number of phone numbers that must be formatted. The next lines contain the numbers themselves: strings consisting of exactly 11 digits.
It is guaranteed that no given number can be split into a country code, an operator or region code, and a subscriber number in more than one way.
Output
Output the numbers given in the input file, formatted according to the rules described in the statement. Output each number on a separate line. Output the numbers in the same order in which they are listed in the input file.
For numbers that cannot be correctly split into a country code, an operator or region code, and a subscriber number, output the word <<Incorrect>> instead.