【Edit Distance】cpp
题目:
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
代码:
class Solution {
public:
int minDistance(string word1, string word2) {
const int n1 = word1.size();
const int n2 = word2.size();
// initialization
vector<vector<int> > dp(n1+,vector<int>(n2+,));
dp[][] = ;
for ( int j=; j<=n2; ++j ) dp[][j] = dp[][j-]+;
for ( int i=; i<=n1; ++i ) dp[i][] = dp[i-][]+;
// dp process
for ( int i=; i<=n1; ++i )
{
for ( int j=; j<=n2; ++j )
{
if ( word1[i-]==word2[j-] )
{
dp[i][j] = dp[i-][j-];
}
else
{
int rep = dp[i-][j-] + ; // replace
int del = dp[i-][j] + ; // delete
int ins = dp[i][j-] + ; // insert
dp[i][j] = std::min(rep,std::min(del, ins));
}
}
}
return dp[n1][n2];
}
};
tips:
不知道这是一个经典的DP案例,确实首次感到DP算法挺精妙的,很多无法想清楚理清楚的事情,交给DP就好了。
参考了两个网上blog的解释:
http://fisherlei.blogspot.sg/2012/12/leetcode-edit-distance.html
http://bangbingsyb.blogspot.sg/2014/11/leetcode-edit-distance.html
dp[i][j]表示s1[0~i-1]与s2[0~j-1]的编辑距离:
根据题意,计算dp[i][j]分如下几种情况:
1. 如果s1[i-1]==s2[j-1] 则不用三种操作,直接dp[i][j] = dp[i-1][j-1]
2. 如果s1[i-1]!=s2[j-1] 则需要在上几步的基础上进行匹配操作:
a) 如果直接选择替换则 dp[i][j] = dp[i-1][j-1] + 1
翻译过来就是:s1[0~i-2]与s2[0~j-2]已经对上了,把s1[i-1]的值换成s2[j-1]的值,替换之;
b) 如果选择删除操作则 dp[i][j] = dp[i-1][j] + 1
翻译过来就是:s1[0~i-2]与s2[0~j-1]已经对上了,这个s1[i-1]就是多余的了,删除之;
c) 如果选择插入操作则 dp[i][j] = dp[i][j-1] + 1
翻译过来就是:s1[0~i-1]与s2[0~j-2]已经对上了,因此少s1当前坐标后面再不上一个s2[j-1]这个值就OK了,插入之;
按照上述的过程,就可以写出来代码了。
为什么每次都加1呢?因为字符要一个一个匹配。
“插入”、“替换”、“删除”什么时候出现是有讲究的。最优的情况就是这些操作单独就可以完成转换,所以要选择出现的情况。
=============================================
第二次过这道题,一开始忘记了word1[i-1]==word2[j-1]的情况,改了之后AC了。
class Solution {
public:
int minDistance(string word1, string word2) {
if ( word1==word2 ) return ;
int dp[word1.size()+][word2.size()+];
fill_n(&dp[][], (word1.size()+)*(word2.size()+), );
for ( int i=; i<=word1.size(); ++i ) dp[i][] = dp[i-][]+;
for ( int i=; i<=word2.size(); ++i ) dp[][i] = dp[][i-]+;
// dp process
for ( int i=; i<=word1.size(); ++i )
{
for ( int j=; j<=word2.size(); ++j )
{
if ( word1[i-]==word2[j-] )
{
dp[i][j] = dp[i-][j-];
continue;
}
// insert
int ins = dp[i][j-]+;
// delete
int del = dp[i-][j]+;
// replace
int rep = dp[i-][j-]+;
dp[i][j] = min(ins,min(del,rep));
}
}
return dp[word1.size()][word2.size()];
}
};
【Edit Distance】cpp的更多相关文章
- [USACO 2012 Mar Silver] Landscaping【Edit Distance】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=126 好题啊好题,一开始就输给了这道题的想法! 先把原始状态以及目标状态换 ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Remove Elements】cpp
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
随机推荐
- String.format()的用法
string.format()用法 2011-06-21 14:58:57| 分类: 工作笔记 | 标签:string format用法 |字号大中小 订阅 1.格式化货币(跟系统的环境有关 ...
- tomcat jdk官网下载教程
Tomcat不同版本官网下载: 1.官网地址:http://tomcat.apache.org/ 2.点击要下载的版本进入下载页,点击Archives进入版本选择页,然后选择对应的版本文件夹,进去后点 ...
- org.hibernate.HibernateException: Unable to get the defa
今天整合SSH框架时出现了这个问题,以前一直没注意,在网上找了一下解决方案,找到了问题的解决方法,特记录如下: 1.原因:在中,javax.persistence.validation.mode默认情 ...
- Head First Python 读书笔记
记录一下这段时间看<Head First Python>记录的一些小知识,只是记了很少一部分,有需要的话以后再添加吧. for循环的使用: for 目标标识符 in 列表: 处理代码 if ...
- IOS 旋转+缩放(手势识别)
@interface NJViewController ()<UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutl ...
- 索引属性 name指定
创建索引时的格式: db.collection.ensureIndex({param},{param}) 其中,第一个是索引的值,之前一直只用到了第一个,第二个参数便是索引的属性 比较重要的属性有: ...
- Mybatis- 配置
主配置文件 properties 第一种 <properties> <property name="jdbc.driver" value="com.my ...
- Linux环境下使用xampp配置php开发环境
XAMPP (Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包.这个软件包原来的名字是LAMPP,但是为 了避免误 解,最新的几个版本就改名为 XAMPP 了.它可以在Win ...
- 阿里云服务器下安装LAMP环境(CentOS Linux 6.3) 安装与配置 php
下面我们一起为服务器安装 PHP,在使用 yum 安装软件包的时候,yum 会去默认的资源库里查看我们要安装的软件包,然后到指定的服务器上下载并安装. 但是有的时候,我们要安装的软件包并没有包含在默认 ...
- 关于JavaScript中的事件代理(例子:ul中无数的li上添加点击事件)
面试题:一个ul中有一千个li,如何给这一千个li绑定一个鼠标点击事件,当鼠标点击时alert出这个li的内容和li的位置坐标xy. 看到这个题目,我们一般首先想到的思路是,for循环,遍历1000次 ...