Changing Phone Numbers
Time limit1sMemory limit128 MB
Given area codes and a sequence of rules (digit duplication, digit swap, area-code change) applied over time, answer queries transforming a phone number from one year to another.
- Level
Hard8 of 10
- Topics
- String, Simulation, Implementation, Hash map
- Solved
- No attempts yet
Problem
You work for OTC, the Olandican Telecommunication Company. Unfortunately, OTC did not make wise decisions when assigning telephone numbers to its clients. For example, it underestimated the growth in demand, so it had to increase the number of digits in the local telephone numbers of Oland (the capital) from six to seven, and then again from seven to eight.
A telephone number consists of two parts: an area code and the local number used within that area. For example, if the area code of Oland city is 021, then a telephone number in that city may look like 0211234567. No area code is a prefix of another area code.
Changing telephone numbers is not easy, because it requires updating several databases containing millions of records. Fortunately, the changes follow only a limited set of rules. In the rules below, the position is counted starting from within the local number, i.e. after removing the area code.
- For every local number under a given area code, repeat the -th digit of the local number once more. For example, under area code
021, repeating the second digit changes0211234567into02112234567. - For every number under a given area code, swap the -th and -th digit of the local number. For example, under area code
021, swapping the second and third digits changes0211234567into0211324567. - Change the area code of a given area to a new value. For example, changing
021into0211changes0211234567into02111234567.
Changing area codes with the third rule always preserves the property that no area code is a prefix of another. Given the area-code information, all telephone numbers, and the set of rules that are applied, write a program that determines the resulting telephone numbers after all changes.
Input
The first part of the input describes the area codes. The first line contains a single integer (), the number of area codes, followed by lines, each of the form:
area-code area-name
area-code is a string of at least and at most digits, and area-name is a string of at least and at most letters (uppercase and lowercase allowed). No two lines share the same area code, and no two lines share the same area name.
The second part describes the rules applied. Its first line contains a single integer (), the number of rules, followed by lines, each of the form:
year rule-info
year is the year in which the rule is applied. Each rule takes effect on the first day of that year, and at most one rule is applied in any given year. Depending on the rule, rule-info has one of the following forms:
1 area-name i2 area-name i3 area-name new-area-code
You may assume the input is always consistent: rule position indices are never out of range, and at no point in time is one area code a prefix of another.
The third part consists of several queries, each of the form:
year1 year2 number
The query asks: given that in year1 there was a telephone number number, what does that number become in year2 (with year1 year2)? You must apply, in order, the rules that were applied in the years from year1 + 1 through year2. The queries end with a line containing three zeros. Every year is a positive integer less than .
When a line contains more than one data item (a number or a name), the items are separated by one or more spaces, and there may be an arbitrary number of leading or trailing spaces. Each data item has at least one character, and each input line has at most characters.
Output
For each query, print on its own line the resulting telephone number after the changes.