动态规划——C编辑最短距离
描述
Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below:
- Deletion: a letter in x is missing in y at a corresponding position.
- Insertion: a letter in y is missing in x at a corresponding position.
- Change: letters at corresponding positions are distinct
Certainly, we would like to minimize the number of all possible operations.
Illustration
A G T A A G T * A G G C
| | | | | | |
A G T * C * T G A C G CDeletion: * in the bottom line
Insertion: * in the top line
Change: when the letters at the top and bottom are distinct
This tells us that to transform x = AGTCTGACGC into y = AGTAAGTAGGC we would be required to perform 5 operations (2 changes, 2 deletions and 1 insertion). If we want to minimize the number operations, we should do it like
A G T A A G T A G G C
| | | | | | |
A G T C T G * A C G C
and 4 moves would be required (3 changes and 1 deletion).
In this problem we would always consider strings x and y to be fixed, such that the number of letters in x is m and the number of letters in yis n where n ≥ m.
Assign 1 as the cost of an operation performed. Otherwise, assign 0 if there is no operation performed.
Write a program that would minimize the number of possible operations to transform any string x into a string y.
Input
The input consists of the strings x and y prefixed by their respective lengths, which are within 1000.
Output
An integer representing the minimum number of possible operations to transform any string x into a string y.
Sample Input
10 AGTCTGACGC
11 AGTAAGTAGGC
Sample Output
4
题目大意: 给出两个字符串X,Y,求出从X——>Y的最小操作次数,只可以删除,添加,修改一个字符。
解题思路:
也是DP中比较经典的问题
d[i][j]表示第一个串到i位置,和第二个串到j位置的最短编辑距离
d[i][j]
如果a[i]==b[j]
d[i][j]=min(d[i-1][j-1],d[i-1][j]+1,d[i][j-1]);
否则d[i][j]=min(d[i-1][j-1]+1,d[i-1][j]+1,d[i][j-1]);
程序代码:
#include <cstdio>
#include <iostream>
using namespace std;
const int M=;
char a[M],b[M] ;
int d[M][M];
int main()
{
int n,i,j,l,r;
while( scanf("%d%s",&l,a+)!=EOF)
{
scanf("%d%s",&r,b+);
int len=max(l,r);
for(i=;i<=len;i++)
{
d[i][]=i;
d[][i]=i;
}
for(i=;i<=l;i++)
for(j=;j<=r;j++)
{
d[i][j]=min(d[i-][j]+,d[i][j-]+);
if(a[i]==b[j])
d[i][j]=d[i-][j-];
else
d[i][j]=min(d[i][j],d[i-][j-]+);
}
printf("%d\n",d[l][r]);
}
return ;
}
动态规划——C编辑最短距离的更多相关文章
- [程序员代码面试指南]递归和动态规划-最小编辑代价(DP)
问题描述 输入 原字符串StrOrg,目标字符串StrTarget,插入.删除.替换的编辑代价ic,dc,rc.输出将原字符串编辑成目标字符串的最小代价. 解题思路 状态表示 dp[i][j]表示把s ...
- poj 3356
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- (5千字)由浅入深讲解动态规划(JS版)-钢条切割,最大公共子序列,最短编辑距离
斐波拉契数列 首先我们来看看斐波拉契数列,这是一个大家都很熟悉的数列: // f = [1, 1, 2, 3, 5, 8] f(1) = 1; f(2) = 1; f(n) = f(n-1) + f( ...
- 编辑距离及其动态规划算法(Java代码)
编辑距离概念描述 编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.一般情况下编辑操作包括: 将一个字符替换成另一个字符: 插入一个字符: 删除一个字 ...
- 【技术文档】《算法设计与分析导论》R.C.T.Lee等·第7章 动态规划
由于种种原因(看这一章间隔的时间太长,弄不清动态规划.分治.递归是什么关系),导致这章内容看了三遍才基本看懂动态规划是什么.动态规划适合解决可分阶段的组合优化问题,但它又不同于贪心算法,动态规划所解决 ...
- [NOIP复习]第三章:动态规划
一.背包问题 最基础的一类动规问题.相似之处在于给n个物品或无穷多物品或不同种类的物品,每种物品仅仅有一个或若干个,给一个背包装入这些物品,要求在不超出背包容量的范围内,使得获得的价值或占用体积尽可能 ...
- 算法笔记1 - 编辑距离及其动态规划算法(Java代码)
转载请标注原链接:http://www.cnblogs.com/xczyd/p/3808035.html 编辑距离概念描述 编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个 ...
- Codeforces 946 D.Timetable-数据处理+动态规划(分组背包) 处理炸裂
花了两个晚上来搞这道题. 第一个晚上想思路和写代码,第二个晚上调试. 然而还是菜,一直调不对,我的队友是Debug小能手呀(真的是无敌,哈哈,两个人一会就改好了) D. Timetable tim ...
- java动态规划问题
这里是简单的动态规划问题.其实,如果我们学过数据结构,应该就接触过动态规划问题,当时一直没有反应过来.我们求最小生成树用的是贪婪算法.而求最短路径就是动态规划.从一个点出发,到另外每个点的最短距离.在 ...
随机推荐
- js--小结⑦---格式转换
- Erlang - Download and Install for Linux
1. 下载 Erlang [huey@huey-K42JE erlang]$ wget http://www.erlang.org/download/otp_src_R16B03.tar.gz 2. ...
- DataView操作DataTable
1.DataView筛选数据 //假设有一个DataTable数据 DataTable dt = new DataTable(); //转成DefaultView DataView dv = dt.D ...
- TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...
- 无软驱加载raid驱动安装windows2003及其他微软操作系统
[转载]http://blog.zol.com.cn/2650/article_2649199.html [另一篇]http://www.blue1000.com/bkhtml/c159/2013-0 ...
- 绘图quartz之加水印
实现在图片上加一个水印 并存在document的路径下 同时在手机相册中也存一份 //首先开启imageContext找到图片 UIGraphicsBeginImageContext( ...
- jQuery 删除元素
通过 jQuery,可以很容易地删除已有的 HTML 元素. 删除元素/内容 如需删除元素和内容,一般可使用以下两个 jQuery 方法: remove() - 删除被选元素(及其子元素) empty ...
- css布局小技巧 2016.03.06
偶遇一个可爱的css布局学习网站,立刻学起来哟- max-width: 当页面左右宽度缩小时,为了避免出现左右滚动条的糟糕体验,就可以用到max-width啦!页面比宽度小时,会自动缩小哦- max- ...
- QT QSettings 操作(导入导出、保存获取信息)*.ini文件详解
1.QSettings基本使用 1.1.生成.ini文件,来点实用的代码吧. QString fileName;fileName = QCoreApplication::applicationDirP ...
- 最优雅的C++跟lua交互.
我先来吐槽一下我们这个项目. 我是做手机游戏的, cocos2dx引擎, lua编码. 这本来是一件很欢快的事情, 因为不用接触C++. C++写久了的人写lua, 就会感觉任督二脉被打通了, 代码写 ...