This page is still under construction.

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

Using sed

Time limit1sMemory limit128 MB

Summary
Find the minimum number of sed-style leftmost non-overlapping replacement operations needed to turn one small string into another, given up to 10 rewrite rules.
Level

Hard8 of 10

Topics
BFS, String matching, Simulation
Solved
No attempts yet

Problem

sed is a Linux utility that replaces occurrences of a string α\alpha with another string β\beta in the strings given as input; here, each input string is a single line of a file. sed performs the following two steps:

  1. Mark non-overlapping occurrences of α\alpha in the input string. (Occurrences of α\alpha may overlap one another in the text, but the marked ones must not overlap.) When there is more than one way to choose a non-overlapping set of occurrences, choose the leftmost ones.
  2. Replace every marked α\alpha with β\beta simultaneously. All other characters are left unchanged.

For example, if α\alpha is aa, β\beta is bca, and the input string is aaxaaa, then running sed gives bcaxbcaa (it cannot be aaxbcaa or bcaxabca). Running sed again on bcaxbcaa gives bcaxbcbca.

You are given nn replacement rules (αi,βi)(\alpha_i, \beta_i) for i=1,2,…,ni = 1, 2, \ldots, n, an initial string γ\gamma, and a target string δ\delta. Using sed, you want to transform γ\gamma into δ\delta with the minimum number of replacement operations.

A single rule (αi,βi)(\alpha_i, \beta_i), as described above, replaces all non-overlapping (leftmost) occurrences of αi\alpha_i in the current string with βi\beta_i at the same time; this counts as one operation. Each rule may be used any number of times, including zero.

Input

The input consists of several test cases. Each test case has the following format:

n
α1 β1
α2 β2
...
αn βn
γ
δ

Here nn is the number of replacement rules. Each αi\alpha_i and βi\beta_i are separated by a space and satisfy 1≤∣αi∣<∣βi∣≤101 \le |\alpha_i| < |\beta_i| \le 10, where ∣s∣|s| denotes the length of the string ss. For all i≠ji \ne j, αi≠αj\alpha_i \ne \alpha_j. Also n≤10n \le 10 and 1≤∣γ∣<∣δ∣≤101 \le |\gamma| < |\delta| \le 10. All strings consist of lowercase letters only. The last line of the input contains a single 00.

Output

For each test case, output the minimum number of replacement operations needed to transform γ\gamma into δ\delta. If γ\gamma cannot be transformed into δ\delta, output −1-1.

Examples3

  1. Example 1

    Input
    2
    a bb
    b aa
    a
    bbbbbbbb
    1
    a aa
    a
    aaaaa
    3
    ab aab
    abc aadc
    ad dee
    abc
    deeeeeeeec
    10
    a abc
    b bai
    c acf
    d bed
    e abh
    f fag
    g abe
    h bag
    i aaj
    j bbb
    a
    abacfaabe
    0
    
    Expected output
    3
    -1
    7
    4
    
  2. Example 2

    Input
    1
    a aaa
    a
    aaa
    0
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    a aa
    a
    aaa
    0
    
    Expected output
    -1