This page is still under construction.

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

Boring Lesson

Time limit1sMemory limit512 MB

Summary
Given strings s and t and a list of n favorite strings, find the minimum edit distance from s to t and the largest subset of favorites that can all appear along a shortest edit path, then output that subset in order.
Level

Hard9 of 10

Topics
Dynamic programming, String, Graph, Shortest path
Solved
No attempts yet

Problem

Ildar is attending a boring online lesson. To have something to do, he transforms strings. Initially, he has a string ss. Ildar wants to obtain a string tt from the string ss in the minimum number of steps. In one step he can:

  • Remove a character from any position.
  • Insert any character at any position. That is, before the first character, between two adjacent characters, or after the last character.
  • Replace the character at any position with any other character.

The minimum number of such steps needed to transform string ss into string tt is also known as the edit distance between ss and tt.

Ildar has nn favorite strings wiw_i. Consider the sequence of strings that occur during the transformation: s=x1s = x_1, x2x_2, \dots, xm−1x_{m - 1}, xm=tx_m = t. Ildar wants as many of the wiw_i as possible to appear in the set {x1,x2,…,xm}\{x_1, x_2, \dots, x_m\}. Help Ildar find the minimum number of steps needed to transform ss into tt and the maximum number of wiw_i that can appear during this process, and print those strings as well.

Input

The first line of input contains the string ss.

The second line of input contains the string tt.

The third line contains a single integer nn (0≤n≤1 0000 \le n \le 1\,000). The following nn lines contain strings wiw_i.

All strings consist of lowercase English letters, are non-empty, and their lengths do not exceed 10 00010\,000. The total length of all strings does not exceed 10 00010\,000. All strings are distinct, including s≠ts \neq t, s≠wis \neq w_i, and t≠wit \neq w_i.

Output

On the first line of output, print two integers: the minimum number of steps needed to transform ss into tt, and the maximum number of strings wiw_i that can appear during the transformation.

After that, print the strings wiw_i that can appear during the transformation, in the order in which they would appear. If there are multiple correct answers, you can print any of them.

Notes

In the second example, one correct transformation is the following:

"longlong" →\rightarrow "longleng" →\rightarrow "dongleng" →\rightarrow "dongleg" →\rightarrow "dongle" →\rightarrow "donble" →\rightarrow "double"

Ildar's favorite strings are highlighted.

Examples2

  1. Example 1

    Input
    cat
    dog
    4
    dot
    pot
    rat
    oat
    
    Expected output
    3 1
    dot
    
  2. Example 2

    Input
    longlong
    double
    3
    doublon
    longleng
    dongle
    
    Expected output
    6 2
    longleng
    dongle