#abc261g. G - Replace
G - Replace
Score : points
问题描述
你将得到两个由小写英文字母组成的字符串 和 。
高桥以字符串 为初始状态。他可以按任意顺序执行 种操作任意次。 第 种操作如下:
支付 的费用。然后,如果当前字符串包含 字符 ,选择其出现的任意一个位置,并用 字符串 替换它。否则,不做任何操作。
请找出使字符串变为 所需的最小总费用。如果无法实现这一目标,则输出 。
以上为通义千问 qwen-max 翻译,仅供参考。
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 .
Sample Input 1
ab
cbca
3
a b
b ca
a efg
Sample Output 1
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.
Sample Input 2
a
aaaaa
2
a aa
a aaa
Sample Output 2
2
Two operations a
aaa
aaaaa
incur a cost of , which is the minimum possible.
Sample Input 3
a
z
1
a abc
Sample Output 3
-1
No sequence of operations makes z
from a
.
update @ 2024/3/10 11:05:00