This page is still under construction.

Parts of this page are still being built. What you see may change.

Address Matching

Time limit3sMemory limit1024 MB

Summary
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:

  1. every address in the first list is matched to exactly one address in the second list,
  2. no address in the second list is matched to more than one address in the first list,
  3. 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 cDc_D, inserting a single character costs cAc_A, 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 NN (1≤N≤201 \le N \le 20) — the number of addresses collected from the students.
  • Line 2: the NN student email addresses, separated by spaces.
  • Line 3: an integer MM (N≤M≤20N \le M \le 20) — the number of addresses received from the teacher.
  • Line 4: the MM teacher addresses, separated by spaces.
  • Line 5: two integers cDc_D and cAc_A (0≤cD,cA≤1060 \le c_D, c_A \le 10^6) — the cost of deleting and of inserting a single character.
  • Line 6: the number KK of distinct characters used in the addresses (1≤K≤601 \le K \le 60).
  • Line 7: exactly KK characters given as a single string with no separators.
  • The next KK lines: a K×KK \times K matrix with KK integers per line. The value ci,jc_{i,j} (0≤ci,j≤1060 \le c_{i,j} \le 10^6) in row ii, column jj is the cost of replacing the ii-th character on line 7 with the jj-th one, and ci,i=0c_{i,i} = 0.

Every address consists of at most 100 characters, and every character occurring in any address is one of the KK characters listed on line 7.

Output

  • Line 1: the minimum total difference over all valid pairings.
  • Line 2: NN space-separated integers v1,…,vNv_1, \dots, v_N, where viv_i (1-indexed) is the position in the teacher's list matched to the ii-th student address. If several pairings achieve the minimum total difference, output the one whose sequence (v1,v2,…,vN)(v_1, v_2, \dots, v_N) is lexicographically smallest.

Examples3

  1. Example 1

    Input
    1
    abcd@abc.cde
    1
    cd@aaabc.dcd
    3 5
    7
    abcde@.
    0 10 10 10 10 10 10
    10 0 10 10 10 10 10
    10 10 0 1 10 10 10
    10 10 1 0 1 10 10
    10 10 10 10 0 10 10
    10 10 10 10 10 0 10
    10 10 10 10 10 10 0
    
    Expected output
    19
    1
    
  2. Example 2

    Input
    3
    ab cd ef
    3
    ab cd ef
    1 1
    6
    abcdef
    0 5 5 5 5 5
    5 0 5 5 5 5
    5 5 0 5 5 5
    5 5 5 0 5 5
    5 5 5 5 0 5
    5 5 5 5 5 0
    
    Expected output
    0
    1 2 3
    
  3. Example 3

    Input
    2
    aaa bbb
    2
    bbb aaa
    1 1
    2
    ab
    0 5
    5 0
    
    Expected output
    0
    2 1