CF 33B String Problem
对每个位置进行操作,求出最终变成相同字母的代价,然后把所有的位上代价加起来,使得总代价最小。res[i][j]表示将字母i+'a'和字母j+'a'变为相同的代价,设最终都变成字母k+'a',那么res[i][j]<?=dp[i][k]+dp[k][j],dp[i][k]表示将字母i+'a'最终变成j+'a'所需的代价,也就是点i到j的最短路径长度,Floyd—Warshall算法可解决。
rec[i][j]记录i和j最终变成的字母-'a'.
#include<stdio.h>
#include<string.h>
#define N 100010
#define M 30
#define INF 9999
#define min(a,b) ((a)>(b)?(b):(a)) int dp[M][M],rec[M][M],res[M][M],vis[M][M];
char s[N],t[N]; int find(int i,int j)
{
if(vis[i][j])
return res[i][j];
int k,ans=res[i][j];
vis[i][j]=vis[j][i]=; for(k=; k<; k++)
if(dp[i][k]+dp[j][k]<ans)
{
ans=dp[i][k]+dp[j][k];
rec[j][i]=rec[i][j]=k;
} return res[i][j]=res[j][i]=ans;
} int main(void)
{
gets(s);
gets(t);
char ch1,ch2;
int n,i,j,k;
for(i=; i<; i++)
for(j=; j<; j++)
if(i==j)vis[i][j],rec[i][j]=j;
else dp[i][j]=INF,res[i][j]=INF,rec[i][j]=-;//将i!=j的点初始状态用INF表示为不可连通 scanf("%d",&n);
getchar(); for(i=; i<=n; i++)
{
int w;
scanf("%c %c %d",&ch1,&ch2,&w);
getchar();
ch1-='a';
ch2-='a';
dp[ch1][ch2]=min(w,dp[ch1][ch2]);
} for(k=; k<; k++)
for(i=; i<; i++)
for(j=; j<; j++)
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]); int te,sum=;
int len1=strlen(s);
int len2=strlen(t);
if(len1-len2)
printf("-1\n");
else
{
for(i=; i<len1; i++)
{
ch1=s[i]-'a';
ch2=t[i]-'a';
te=find(ch1,ch2);
if(te>=INF)
return printf("-1\n"),;
else
{
sum+=te;
s[i]=rec[ch1][ch2]+'a';
}
}
printf("%d\n",sum);
puts(s);
}
return ;
}
CF 33B String Problem的更多相关文章
- hdu String Problem(最小表示法入门题)
hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...
- HDU 3374 String Problem(KMP+最大/最小表示)
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 【HDU3374】 String Problem (最小最大表示法+KMP)
String Problem Description Give you a string with length N, you can generate N strings by left shift ...
- HDOJ3374 String Problem 【KMP】+【最小表示法】
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- String Problem hdu 3374 最小表示法加KMP的next数组
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ACM-ICPC2018南京赛区 Mediocre String Problem
Mediocre String Problem 题解: 很容易想到将第一个串反过来,然后对于s串的每个位置可以求出t的前缀和它匹配了多少个(EXKMP 或者 二分+hash). 然后剩下的就是要处理以 ...
- hdu3374 String Problem【最小表示法】【exKMP】
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 5772 String problem 最大权闭合子图
String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...
随机推荐
- Linux下crontab命令详解
crontab -e编辑定时任务 * * * shell.sh 从左到右依次是:分钟.小时.天.周.月
- 组合模式(Composite Pattern)
组合模式定义:组合模式允许你将对象组合成树形结构来表现"整体/局部"层次结构,组合能让客户以一致的方式处理个别对象以及对象组合. 当涉及到如:菜单,子菜单之类的问题时,会自然而然的 ...
- Atom编辑器入门到精通(二) 插件的安装和管理
在本节中我们会学习如果安装和使用插件插件是Atom中一个非常重要的组成部分,很多功能都是以插件形式存在的.比如上篇文章中提到的目录树和设置等窗口都是通过默认安装的插件来实现的. 查看已安装的插件 打开 ...
- C# Callback 回调实用讲解 [原创]
这个是2011年写的一个技术贴,现在看可能有点过时了,有兴趣的朋友也可以看一下. 一. 描述 在开发winform程序时不会考虑页面刷新问题,只要控制好线程别导致假死就ok了,但是在开发web页面程序 ...
- ImageButton 在IE 10 下的异常
最近在项目中遇到一个棘手问题,在IE10中,一些图片按钮点了毫无反应,其他浏览器(包括IE9)都正常:查看后台,发现如下异常信息: Input string was not in a correct ...
- ios tableview 上加 textfiled
ios tableview 上加 textfiled 首先附上我项目中用曾经用到的几张图 并说明一下我的用法: 图1: 图2: 图3: 心在你我说一下 我当初的实现 方法 ,希望能给你们一些 启 ...
- 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效
基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...
- Malformed network data
Malformed network data
- HOW TO: Creating your MSI installer using Microsoft Visual Studio* 2008
Quote from: http://software.intel.com/en-us/articles/how-to-creating-your-msi-installer-using-visual ...
- (转)Libevent(3)— 基础库
转自:http://name5566.com/4202.html 参考文献列表:http://www.wangafu.net/~nickm/libevent-book/ 此文编写的时候,使用到的 Li ...