Codeforces Beta Round 33(Codeforces 格式)
B
字符串问题
String Problem
Codeforces
1800
原题
← AC →

字符串问题AI (CI翻译)

Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character $*A*_{*i*}$ in one of the strings into arbitrary character $*B*_{*i*}$, but he has to pay for every move a particular sum of money, equal to $*W*_{*i*}$. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings.

男孩 Valera 喜欢字符串。他尤其喜欢相同的字符串。因此,闲暇时 Valera 会玩下面这个游戏:他任意取两个由小写拉丁字母组成的字符串,并尝试让它们变得相同。根据游戏规则,每次操作中,Valera 可以将其中一个字符串里的一个任意字符 $*A*_{*i*}$ 改成任意字符 $*B*_{*i*}$,但每次操作都需要支付一笔费用 $*W*_{*i*}$。他可以进行任意多次操作。Valera 很节俭,从不浪费钱,因此他请你这位经验丰富的程序员帮忙回答:要让两个字符串相同,Valera 最少需要多少钱?AI (CI翻译)

Input

输入格式

AI (CI翻译)

The first input line contains two initial non-empty strings $*s*$ and $*t*$, consisting of lower case Latin letters. The length of each string doesn't exceed $10^{5}$. The following line contains integer $*n*$ ($0 ≤ *n* ≤ 500$) — amount of possible changings. Then follow $*n*$ lines, each containing characters $*A*_{*i*}$ and $*B*_{*i*}$ (lower case Latin letters) and integer $*W*_{*i*}$ ($0 ≤ *W*_{*i*} ≤ 100$), saying that it's allowed to change character $*A*_{*i*}$ into character $*B*_{*i*}$ in any of the strings and spend sum of money $*W*_{*i*}$.

输入的第一行包含两个非空字符串 $*s*$ 和 $*t*$,均由小写拉丁字母组成。每个字符串的长度不超过 $10^{5}$。下一行包含整数 $*n*$($0 ≤ *n* ≤ 500$),表示可进行的字符替换规则数量。接下来的 $*n*$ 行中,每行包含字符 $*A*_{*i*}$ 和 $*B*_{*i*}$(小写拉丁字母)以及整数 $*W*_{*i*}$($0 ≤ *W*_{*i*} ≤ 100$),表示可以在任意一个字符串中将字符 $*A*_{*i*}$ 替换为字符 $*B*_{*i*}$,费用为 $*W*_{*i*}$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.

如果存在可行方案,输出题目所求的最小费用以及替换后得到的字符串。否则,在唯一一行中输出 -1。如果答案不唯一,输出任意一个。AI (CI翻译)

样例

Input #1
uayd
uxxd
3
a x 8
x y 13
d c 3
Output #1
21
uxyd
InputCopy OutputCopy
Input #2
a
b
3
a b 2
a b 3
b a 5
Output #2
2
b
InputCopy OutputCopy
Input #3
abc
ab
6
a b 4
a b 7
b a 8
c b 11
c a 3
a c 0
Output #3
-1
InputCopy OutputCopy
题目信息
题号
33B
时间限制
2 s
内存限制
256 MB
标签
最短路

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。