AtCoder Beginner Contest 261
翻译中…
Problem Statement
翻译中…
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$.
翻译中…
Constraints
翻译中…
$1\leq |S|\leq |T|\leq 50$$1\leq K\leq 50$$C_i$isa,b,$\ldots$, orz.$1\leq |A_i|\leq 50$$S$,$T$, and$A_i$are strings consisting of lowercase English letters.$C_i\neq A_i$, regarding$C_i$as a string of length$1$.- All pairs
$(C_i,A_i)$are distinct.
翻译中…
Input
翻译中…
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
翻译中…
Output
翻译中…
Print the minimum total cost needed to make the string equal $T$. If it is impossible to do so, print $-1$.
翻译中…
样例
Input #1
ab
cbca
3
a b
b ca
a efg
Output #1
4
Starting with $S=$`ab`, Takahashi can make $T=$`cbca` in four operations as follows:
- Replace the $1$-st character `a` in `ab` with `b` (Operation of the $1$-st kind). The string is now `bb`.
- Replace the $2$-nd character `b` in `bb` with `ca` (Operation of the $2$-nd kind). The string is now `bca`.
- Replace the $1$-st character `b` in `bca` with `ca` (Operation of the $2$-nd kind). The string is now `caca`.
- Replace the $2$-nd character `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.
Input #2
a
aaaaa
2
a aa
a aaa
Output #2
2
Two operations `a` $\to$ `aaa` $\to$ `aaaaa` incur a cost of $2$, which is the minimum possible.
Input #3
a
z
1
a abc
Output #3
-1
No sequence of operations makes $T=$`z` from $S=$`a`.
题目信息
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。