动态规划——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动态规划问题
这里是简单的动态规划问题.其实,如果我们学过数据结构,应该就接触过动态规划问题,当时一直没有反应过来.我们求最小生成树用的是贪婪算法.而求最短路径就是动态规划.从一个点出发,到另外每个点的最短距离.在 ...
随机推荐
- 自己写的自动生成动态边框的jquery小插件
思路就是在元素四周添加<ul>列表,然后周期性地改变它的颜色,实现动态的效果,不支持ie7.ie8 预览链接http://gorey.sinaapp.com/myBorder/border ...
- java构造函数也可以用private开头
private 构造函数一般用于Singleton模式,指的是整个应用只有本类的一个对象,一般这种类都有一个类似getInstance()的方法!下面是一个Singleton的例子:public cl ...
- 遍历id,根据id作为条件循环查询。
string id = "OE09924008161405102,OE36765709071405102,OE87852044171405102,OE09924008161405102&qu ...
- jdbc - 连接数据库的url
MySql: driver:com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/database_name SQL Server 2008: ...
- 【转】iOS使用NSMutableAttributedString实现富文本
iOS使用NSMutableAttributedString实现富文本 在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘 ...
- wpf 自定义RadioButton控件样式
实现的效果为: 我感觉来自定义RadioButton样式和定义button空间的样式差不多,只是类型不同而已. 接下来分析一下样式代码: <!--自定义单选按钮样式--> & ...
- hdoj 2191(多重背包)
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/ ...
- mysql基础操作整理(一)
显示当前数据库 mysql> select database(); +------------+ | database() | +------------+ | test | +-------- ...
- centos7/RHEL7安装LibreOffice
1.下载 wget http://download.documentfoundation.org/libreoffice/testing/4.4.0/rpm/x86_64/LibreOfficeDev ...
- 15_RHEL7挂载NTFS分区
1.下载ntfs-3g wget https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2015.3.14.tgz 2.安装 tar -zxvf ntfs-3 ...