CF33B.String Problem

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 AiA_{i} in one of the strings into arbitrary character BiB_{i} , but he has to pay for every move a particular sum of money, equal to WiW_{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.

输入格式

The first input line contains two initial non-empty strings ss and tt , consisting of lower case Latin letters. The length of each string doesn't exceed 10510^{5} . The following line contains integer nn ( 0n5000\le n\le 500 ) — amount of possible changings. Then follow nn lines, each containing characters AiA_{i} and BiB_{i} (lower case Latin letters) and integer WiW_{i} ( 0Wi1000\le W_{i}\le100 ), saying that it's allowed to change character AiA_{i} into character BiB_{i} in any of the strings and spend sum of money WiW_{i} .

输出格式

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

    uayd
    uxxd
    3
    a x 8
    x y 13
    d c 3
    

    输出#1

    21
    uxyd
    
  • 输入#2

    a
    b
    3
    a b 2
    a b 3
    b a 5
    

    输出#2

    2
    b
    
  • 输入#3

    abc
    ab
    6
    a b 4
    a b 7
    b a 8
    c b 11
    c a 3
    a c 0
    

    输出#3

    -1
    
首页