【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 ...
随机推荐
- SpringMVC项目的快速搭建
Spring MVC提供了一个DispatcherServlet来开发Web应用.在Servlet2.5及2以下的时候只要在web.xml下配置<servlet>元素即可. 在Servle ...
- 《Python高效开发实战》实战演练——基本视图3
在完成Django项目和应用的建立后,即可以开始编写网站应用代码,这里通过为注册页面显示一个欢迎标题,来演示Django的路由映射功能. 1)首先在djangosite/app/views.py中建立 ...
- bt5 r3下metasploit连接postgresql数据库
一.查看PostgreSQL使用的端口,默认为7337 #: netstat -tnpl |grep postgres 二.查看Msf配置,里面有默认的用户名和密码 默认配置文件:/opt/metas ...
- php使用GD库实现图片水印和缩略图——给图片添加图片水印
今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...
- hdu-1272 小希的迷宫---并查集或者DFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 题目大意: Problem Description 上次Gardon的迷宫城堡小希玩了很久(见 ...
- BSGS算法初探
前言 \(BSGS\)算法,全称\(Baby\ Step\ Giant\ Step\),即大小步算法.某些奆佬也称其为拔(Ba)山(Shan)盖(Gai)世(Shi)算法. 它的主要作用是求解形式如\ ...
- 2017.12.2 用java做一个日历
1.先判断输入的日期是否为闰年 2.在判断输入的月份是否为2月 3.在获取输入的年份和月份的1月1日 的列数 4.在输出 import java.util.*; public class demo{ ...
- JS let和const关键字
ES2015 引入了两个重要的 JavaScript 新关键词:let 和 const. Let关键字 1.用于作用域:块作用域,循环作用域,函数作用域,全局作用域, 在 ES2015 之前,Java ...
- hibernate系列之一
通过自己不断的学习框架以及相关知识的学习,自己学会总结了学习路上遇到的一些问题以及疑惑,自己现在跟着相关的学习资料又进行了一些总结和实践,希望通过自己走过的学习之路能够帮助小伙伴们解决一些学习上问题或 ...
- jsp引用servlet生成的验证码代码演示
此演示代码主要包括以下三部分:1.checkCode.java:用于生成验证码2.checkCodeServler3.check.jsp 验证 下面是checkCode.java的内容: 复制代码代码 ...