【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 ...
随机推荐
- Android中intent相关,setFlag(xx);
intent.setFlags(参数)://参数用法如下 :FLAG_ACTIVITY_CLEAR_TOP: 例如现在的栈情况为:A B C D .D此时通过intent跳转到B,如果这个intent ...
- Mysql数据库操作语句总结(一)
下面的内容来源于链接 https://www.cnblogs.com/bchjazh/p/5997728.html, 个人在此基础上进一步添加了一点东西. 个人操作数据库: mysql 5.5.2 ...
- python 之 BeautifulSoup 常用提取
一.bs4信息提取后返回的数据类型 soup.find('tbody') ---> 返回结构是一个bs4.element.Tag soup.find('tbody').children ---& ...
- postman传递参数的问题
postman是一款通过post或者get发送请求测试代码的工具 如果是类的话,就选择JSON格式,如果是一个字段的方法,就直接写入方法值就好了比如 public PageResult<Info ...
- 动态规划专题(三)——数位DP
前言 数位\(DP\) 真的是最恶心的\(DP\). 简介 看到那种给你两个数,让你求这两个数之间符合条件的数的个数,且这两个数非常大,这样的题目一般就是 数位\(DP\) 题. 数位\(DP\)一般 ...
- iOS 真机报错 Command CodeSign failed with a nonzero exit code
今天在网上下载的一个小demo,在模拟器上正常运行,但是在真机上报错: 解决方式: 1.打开钥匙串:2.锁住login keychain: 3.再把它解锁. 然后就能真机上正常运行了,
- C/C++语言补缺 宏- extern "C"-C/C++互调
1. 宏中的# 宏中的#的功能是将其后面的宏参数进行字符串化操作(Stringizing operator),简单说就是在它引用的宏变量的左右各加上一个双引号. 如定义好#define STRING( ...
- 第七章 动态创建HTML内容
javascript也可以改变网页的结构和内容 document.write()方法 可以方便快捷地把字符串插入到文档里 document.write("<strong>hell ...
- python_71_json序列化1
#序列化:序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程. #本例把字典数据类型存成字符串存在硬盘 #文件只能存字符串和二进制码,字典之类的不可以 info={ ...
- react的redux无状态组件
Provider功能主要为以下两点: 在原应用组件上包裹一层,使原来整个应用成为Provider的子组件 接收Redux的store作为props,通过context对象传递给子孙组件上的connec ...