Score : $600$ points
You are given two strings $S$ and $T$ consisting of lowercase English letters.
Takahashi starts with the string $S$. He can perform $K$ kinds of operations any number of times in any order.
The $i$-th operation is the following:
Pay a cost of $1$. Then, if the current string contains the character $C_i$, choose one of its occurrences and replace it with the string $A_i$. Otherwise, do nothing.
Find the minimum total cost needed to make the string equal $T$. If it is impossible to do so, print $-1$.
a, b,$\ldots$, or z.Input is given from Standard Input in the following format:
$S$ $T$ $K$ $C_1$ $A_1$ $C_2$ $A_2$ $\vdots$ $C_K$ $A_K$
Print the minimum total cost needed to make the string equal $T$. If it is impossible to do so, print $-1$.
ab cbca 3 a b b ca a efg
4
Starting with $S=$ab, Takahashi can make $T=$cbca in four operations as follows:
a in ab with b (Operation of the $1$-st kind). The string is now bb.b in bb with ca (Operation of the $2$-nd kind). The string is now bca.b in bca with ca (Operation of the $2$-nd kind). The string is now caca.a in caca with b (Operation of the $1$-st kind). The string is now cbca.Each operation incurs a cost of $1$, for a total of $4$, which is the minimum possible.
a aaaaa 2 a aa a aaa
2
Two operations a $\to$ aaa $\to$ aaaaa incur a cost of $2$, which is the minimum possible.
a z 1 a abc
-1
No sequence of operations makes $T=$z from $S=$a.