codeforces 33B String Problem

本文介绍了一个字符串转换游戏的算法解决方案,玩家需将两个字符串变为相同,通过改变字符来达到目的,并尽量减少消耗。文章详细解析了如何使用最短路径算法预先计算每个字符之间的最小转换成本,最终实现整体转换成本的最小化。
B. String Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

题解:
用最短路径打表求出每个字母变到另一个字母的最小花费,
然后遍历字符串,时间复杂度26*strlen(t);
代码:
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
#define inf 0xf3f3f3f
int d[27][27];
char t1[100007],t2[100007];
int head[107],tol=0,h[100007];
bool vis[1007];
queue<int>P;
struct node
{
	int to,cost,next;
}rode[100007];
void add(int a,int b,int c)
{
	rode[tol].to=b;
	rode[tol].cost=c;
	rode[tol].next=head[a];
	head[a]=tol++;
}
void spfa(int st)
{
	memset(vis,0,sizeof(vis));
	vis[st]=1;d[st][st]=0;
	P.push(st);
	while(!P.empty())
	{
		int v=P.front();P.pop();
		vis[v]=0;
		for(int i=head[v];i!=-1;i=rode[i].next)
		{
			node e=rode[i];
			if(d[st][e.to]>d[st][v]+e.cost)
			{
				d[st][e.to]=d[st][v]+e.cost;
				if(!vis[e.to])
				{
					vis[e.to]=1;
					P.push(e.to);
				}
			}
		}
	}
}
int main()
{
	scanf("%s",&t1);
	scanf("%s",&t2);
	memset(d,inf,sizeof(d));
	int m;scanf("%d",&m);
	memset(head,-1,sizeof(head));
	while(m--)
	{
		char x1[2],x2[2];int z;scanf("%s%s%d",&x1,&x2,&z);
		add(x1[0]-'a',x2[0]-'a',z);
	}
	for(int i=0;i<26;i++)
	{
		spfa(i);
	}
	long long ans=0;
	int L=strlen(t1);
	if(L!=strlen(t2))
	{
		printf("-1\n");
		return 0;
	}
	for(int i=0;i<L;i++)
	{
		if(t1[i]!=t2[i])
		{
			int mi=0xf3f3f3f+3,id;
			int k1=t1[i]-'a',k2=t2[i]-'a';
			for(int j=0;j<26;j++)
			{
				if(mi>d[k1][j]+d[k2][j])
				{
					mi=d[k1][j]+d[k2][j];
					id=j;
				}
			}
			h[i]=id;
			ans+=mi;
		}
		else h[i]=t1[i]-'a';
	}
	if(ans>=inf)printf("-1\n");
	else 
	{
		printf("%lld\n",ans);
		for(int i=0;i<L;i++)
			printf("%c",h[i]+'a');
	}
	printf("\n");
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值