atcoder#ABC261G. [ABC261G] Replace
[ABC261G] Replace
Score : points
Problem Statement
You are given two strings and consisting of lowercase English letters.
Takahashi starts with the string . He can perform kinds of operations any number of times in any order. The -th operation is the following:
Pay a cost of . Then, if the current string contains the character , choose one of its occurrences and replace it with the string . Otherwise, do nothing.
Find the minimum total cost needed to make the string equal . If it is impossible to do so, print .
Constraints
- is
a
,b
,, orz
. - , , and are strings consisting of lowercase English letters.
- , regarding as a string of length .
- All pairs are distinct.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum total cost needed to make the string equal . If it is impossible to do so, print .
ab
cbca
3
a b
b ca
a efg
4
Starting with ab
, Takahashi can make cbca
in four operations as follows:
- Replace the -st character
a
inab
withb
(Operation of the -st kind). The string is nowbb
. - Replace the -nd character
b
inbb
withca
(Operation of the -nd kind). The string is nowbca
. - Replace the -st character
b
inbca
withca
(Operation of the -nd kind). The string is nowcaca
. - Replace the -nd character
a
incaca
withb
(Operation of the -st kind). The string is nowcbca
.
Each operation incurs a cost of , for a total of , which is the minimum possible.
a
aaaaa
2
a aa
a aaa
2
Two operations a
aaa
aaaaa
incur a cost of , which is the minimum possible.
a
z
1
a abc
-1
No sequence of operations makes z
from a
.