最小编辑代价
来源:互联网 发布:nginx优化 2016 编辑:程序博客网 时间:2024/06/15 22:30
对于两个字符串A和B,我们需要进行插入、删除和修改操作将A串变为B串,定义c0,c1,c2分别为三种操作的代价,请设计一个高效算法,求出将A串变为B串所需要的最少代价。
给定两个字符串A和B,及它们的长度和三种操作代价,请返回将A串变为B串所需要的最小代价。保证两串长度均小于等于300,且三种代价值均小于等于100。
测试样例:
"abc",3,"adc",3,5,3,100
返回:8
import java.util.*;
public class MinCost {
public int findMinCost(String A, int n, String B, int m, int c0, int c1, int c2) {
// write code here
char aa[] = A.toCharArray();
char bb[] = B.toCharArray();
int row =aa.length+1;
int col = bb.length+1;
int arr [][] = new int[row][col];
for (int i =0;i<row;i++){
arr[i][0] = c1*i;
}
for(int i =0;i<col;i++){
arr[0][i] = c0*i;
}
for(int i =1;i<row;i++){
for(int j =1;j<col;j++){
if(aa[i-1]==bb[j-1]){
arr[i][j] = arr[i-1][j-1];
}else {
arr [i][j] = arr[i-1][j-1]+c2;
}
arr[i][j] = Math.min(arr[i][j],arr[i-1][j]+c1);
arr[i][j] = Math.min(arr[i][j],arr[i][j-1]+c0);
}
}
return arr[row-1][col-1];
}
}
阅读全文
0 0
- 最小编辑代价
- 最小编辑代价
- 最小编辑代价
- 最小编辑代价
- godfrey_88 最小编辑代价
- 最小编辑代价
- 最小编辑代价
- 最小编辑代价
- 【动态规划】最小编辑代价
- 最小编辑代价---动态规划
- 最小编辑代价 动态规划
- 最小编辑代价(动态规划)
- 最小编辑代价(动态规划)
- 最小编辑代价(动态规划)
- 递归与动态规划---最小编辑代价
- 动态规划系列问题-最小编辑代价
- 最小代价
- 最小代价;;;;
- Java入门总结
- JAVA8新特性[第六季]-新时间日期 API
- MySQL数据类型和常用字段属性
- PHP设计模式—创建型模式杂谈
- 悬浮广告
- 最小编辑代价
- Maven-(安装、配置)
- 读书笔记之深入理解Java虚拟机
- TextView的跑马灯效果
- HADOOP简明教程——HDFS集群搭建实战
- IDEA下Maven项目整合Spring和MyBatis出现jdbc.properties is invalid;前言中不允许有内容
- 分布式架构
- Python爬虫学习--urllib的基本使用笔记
- js实现左右切换页面