Address Matching
Time limit3sMemory limit1024 MB
Match each student address to a distinct teacher address with minimum total weighted edit distance, and among optimal matchings output the lexicographically smallest index sequence.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Bit manipulation, String matching
- Solved
- No attempts yet
Problem
Researcher Uuno ran a survey among students, asking each of them to write down their email address. Many of the handwritten addresses turned out to be very hard to read, so Uuno asked the class teacher for a separate list of every student's address in order to cross-check. He now wants to pair up the two lists so that all three of the following conditions hold:
- every address in the first list is matched to exactly one address in the second list,
- no address in the second list is matched to more than one address in the first list,
- among all pairings that satisfy conditions 1 and 2, the total difference of the matched pairs is minimized.
The difference between two addresses is defined as follows. Any address can be turned into another one by inserting, deleting, and replacing characters. Each operation has a fixed cost: deleting a single character costs , inserting a single character costs , and replacing one character with another costs a value given in the input. The difference of two addresses is the minimum total cost of transforming the second address into the first.
Find a pairing that satisfies the three conditions. If several pairings achieve the minimum total difference, output the lexicographically smallest one.
Input
- Line 1: an integer () — the number of addresses collected from the students.
- Line 2: the student email addresses, separated by spaces.
- Line 3: an integer () — the number of addresses received from the teacher.
- Line 4: the teacher addresses, separated by spaces.
- Line 5: two integers and () — the cost of deleting and of inserting a single character.
- Line 6: the number of distinct characters used in the addresses ().
- Line 7: exactly characters given as a single string with no separators.
- The next lines: a matrix with integers per line. The value () in row , column is the cost of replacing the -th character on line 7 with the -th one, and .
Every address consists of at most 100 characters, and every character occurring in any address is one of the characters listed on line 7.
Output
- Line 1: the minimum total difference over all valid pairings.
- Line 2: space-separated integers , where (1-indexed) is the position in the teacher's list matched to the -th student address. If several pairings achieve the minimum total difference, output the one whose sequence is lexicographically smallest.