String Problem

题目链接(点击)

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 Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. 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.

Input

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 105. The following line contains integer n (0 ≤ n ≤ 500) — amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin letters) and integer Wi (0 ≤ Wi ≤ 100), saying that it's allowed to change character Ai into character Bi in any of the strings and spend sum of money Wi.

Output

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.

Examples

input

Copy

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

output

Copy

21
uxyd

input

Copy

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

output

Copy

2
b

input

Copy

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

output

Copy

-1

思路:

·  开始看题目没看太仔细,没感觉是最短路,因为我只是考虑了上下两个字母之间转换:

a

b

a→b 或者是 b→a

没考虑到  a→c→d→e

b→e 这种情况

·  看懂以后就知道是模板题了,有一点不同的是:

要将字符转换为数字表示最短路,在判断a变为什么的时候要先将a到其余25个点的最短路存进二维数组 (其余字母同理),      下面是代码:

        for(int i=1;i<=26;i++){
diji(i);
for(int j=1;j<=26;j++){
if(i==j){
num[i][j]=0;
}
else{
num[i][j]=dis[j];
}
}
}

·  想知道a最终变成什么就要判断到其余各点距离的最小值:

如果minn==MAX 即表示该点无法通过条件转化成其他字母,要输出 -1

否则 sum+=minn

·  但要注意:

两次输入相同的字母关系要取最小的 进行 addnode(将两个点用链式前向星连起来)

AC代码:

#include<stdio.h>
#include<string.h>
const int MAX=1e5;
struct node{
int to;
int len;
int next;
}edge[MAX+5];
int ans;
int head[MAX+5];
void addnode(int u,int v,int w)
{
edge[ans].to=v;
edge[ans].len=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void allbegin()
{
memset(head,-1,sizeof(head));
ans=0;
}
int vis[MAX+5],dis[MAX+5];
void diji(int s)
{
for(int i=1;i<=26;i++){
dis[i]=MAX;
vis[i]=0;
}
dis[s]=0;
while(1){
int k=-1,len=MAX;
for(int i=1;i<=26;i++){
if(!vis[i]&&len>dis[i]){
k=i;
len=dis[i];
}
}
if(k==-1){
break;
}
vis[k]=1;
for(int j=head[k];~j;j=edge[j].next){
int t=edge[j].to;
if(!vis[t]&&dis[t]>edge[j].len+dis[k]){
dis[t]=edge[j].len+dis[k];
}
}
}
} //上面是迪杰斯特拉模板
int main()
{
int num[55][55],n;
char a[MAX+5],b[MAX+5],c[MAX+5];
memset(num,-1,sizeof(num));
scanf("%s%s",a,b);
int la=strlen(a);
int lb=strlen(b);
scanf("%d",&n);
allbegin();
for(int i=1;i<=n;i++){
int w;
char u[5],v[5];
scanf("%s%s%d",u,v,&w);
int u1=u[0]-'a'+1;
int v1=v[0]-'a'+1;
if(num[u1][v1]==-1||num[u1][v1]>w){
addnode(u1,v1,w);
}
}
if(la!=lb){ //长度不等 直接输出-1
printf("-1\n");
}
else{ //将26个字母任意两点距离打表
for(int i=1;i<=26;i++){
diji(i);
for(int j=1;j<=26;j++){
if(i==j){
num[i][j]=0;
}
else{
num[i][j]=dis[j];
}
}
}
int sum=0;
for(int i=0;i<la;i++){
int a1=a[i]-'a'+1;
int b1=b[i]-'a'+1;
if(a1==b1){
c[i]=a[i];
}
else{ //暴力找出与两个字符 转换值之和最小的 字母(数字代表字母)
int minn=MAX;
for(int j=1;j<=26;j++){
if(num[a1][j]+num[b1][j]<minn){
c[i]='a'+j-1;
minn=num[a1][j]+num[b1][j];
}
}
if(minn==MAX){ //没有条件将其转换成其他值 结果任然是MAX 输出-1
printf("-1\n");
return 0;
}
else{
sum+=minn;
}
}
}
printf("%d\n",sum);
puts(c);
}
return 0;
}

String Problem(模板)【最短路】的更多相关文章

  1. HDOJ3374 String Problem 【KMP】+【最小表示法】

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. hdu String Problem(最小表示法入门题)

    hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...

  3. HDU 3374 String Problem(KMP+最大/最小表示)

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. 【HDU3374】 String Problem (最小最大表示法+KMP)

    String Problem Description Give you a string with length N, you can generate N strings by left shift ...

  5. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  6. String Problem hdu 3374 最小表示法加KMP的next数组

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. ACM-ICPC2018南京赛区 Mediocre String Problem

    Mediocre String Problem 题解: 很容易想到将第一个串反过来,然后对于s串的每个位置可以求出t的前缀和它匹配了多少个(EXKMP 或者 二分+hash). 然后剩下的就是要处理以 ...

  8. hdu3374 String Problem【最小表示法】【exKMP】

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. hdu 5772 String problem 最大权闭合子图

    String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...

随机推荐

  1. js动态添加iframe,自适应页面宽高

    function showIframe(url,w,h){ //添加iframe var if_w = w; var if_h = h; $("<iframe width='" ...

  2. Linux学习(二):makefile

    编译命令: gcc -o exefile src.c (将src.c编译,链接为exefile可执行文件) gcc -o obj.o -c src.c (将src.c编译为obj.o目标文件) mak ...

  3. [JavaWeb基础] 006.Struts1的配置和简单使用

    1.框架简介 采用Struts能开发出基于MVC(Model-View-Controller)设计模式的应用构架,用于快速开发Java Web应用.Struts实现的重点在C(Controller), ...

  4. BUUCTF WEB

    BUUCTF 几道WEB题WP 今天做了几道Web题,记录一下,Web萌新写的不好,望大佬们见谅○| ̄|_ [RoarCTF 2019]Easy Calc 知识点:PHP的字符串解析特性 参考了一下网 ...

  5. NIO 的非阻塞式网络通信

    1.阻塞与非阻塞   ①  传统的 IO 流都是阻塞式的.也就是说,当一个线程调用 read() 或 write()时, 该线程被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务. 因 ...

  6. return break 和continue在for循环中的不同作用

    平时自己经常在函数里见到return,在switch语句中使用break,而continue则用的不多. 其实这三者都能在for循环中发挥不同的作用,让代码更加灵活. 先说return return是 ...

  7. C#线程 入门

    Threading in C#   第一部分: 入门 介绍和概念 C#支持通过多线程并行执行代码.线程是一个独立的执行路径,能够与其他线程同时运行.C#客户端程序(控制台,WPF或Windows窗体) ...

  8. & 加密

    接口参数中sign加密方式: 1. 签名算法使用SHA256: 2. 服务方和消费方都需要校验签名: 3. 签名生成步骤: 第一步,设所有发送或者接收到的数据为集合M1,将集合M1内非空参数值的参数按 ...

  9. ionic3跳转页面的方法

    ionic3很好很强大,有人喷有人赞.不想参与其中,个人认为如果能很好的满足需求,好坏都是无所谓的,最合适的才是最好的.总结下最近使用ionic3的一些知识点,方便以后查询.多句嘴:会ionic3和只 ...

  10. Java实现 LeetCode 443 压缩字符串

    443. 压缩字符串 给定一组字符,使用原地算法将其压缩. 压缩后的长度必须始终小于或等于原数组长度. 数组的每个元素应该是长度为1 的字符(不是 int 整数类型). 在完成原地修改输入数组后,返回 ...