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

Hide Tags

Dynamic Programming String

 

 
 
一个动态规划的题目,算是简单,不过开始考虑的不够完善。设一个二维数组a[i][j] 其中i 表示 word1 0 to i 字符串,i=0 的时候 表示“”,j 同理,那么a 中每项表示 i 字符串 转换到 j 字符串的最少步骤。
假如 我们已经知道了  <i-1,j-1>  <i-1,j> and  <i,j-1> 那么 <i,j>  可以如下求得:
  • word1[i] == word2[j] ,那么 可以看作 i-1 字符串 和 j-1 字符串各加了一个相同字符,所以<i,j> = <i-1,j-1>
  • word1[i] != word2[j]
    • 对于<i-1,j-1>,即两字符串后面都加了一个字符且不同,那么 replace 一次就行,所以<i,j> = <i-1,j-1>+1
    • 对于<i,j-1>,即只在 j-1 字符串后面加了一个字符,那么delete 一次就行,<i,j> = <i,j-1>+1
    • 对于<i-1,j>,同<i,j-1>
    • 所以 <i,j> 应该去上面3者最小值
  • 填满整个a 之后 <len1,len2> 为输出结果。

注意项:

  • a 二维数组需要考虑字符串为""的初始化,所以维度应该+1.
  • 我使用的是堆里面的空间,leetcode  可以直接使用栈空间创建,即不需要new。

我写的如下:

 #include <iostream>
#include <string>
#include <memory.h>
using namespace std; class Solution {
public:
int minDistance(string word1, string word2) {
int len1 = word1.length(),len2=word2.length();
if(len1==) return len2;
if(len2==) return len1;
int **dpmap = new int *[len1+];
dpmap[] =new int[(len1+)*(len2+)];
memset(dpmap[],,sizeof(int)*(len1+)*(len2+));
for(int i= ;i<=len1;i++)
dpmap[i] = dpmap[i-]+len2+;
for(int i=;i<=len1;i++)
dpmap[i][] = i;
for(int j=;j<=len2;j++)
dpmap[][j] = j;
for(int i=;i<=len1;i++){
for(int j=;j<=len2;j++){
if(word1[i-]==word2[j-]) dpmap[i][j]=dpmap[i-][j-];
else{
dpmap[i][j]=(dpmap[i-][j]>dpmap[i][j-]?dpmap[i][j-]:dpmap[i-][j])+;
if(dpmap[i-][j-]+<dpmap[i][j])
dpmap[i][j] = dpmap[i-][j-]+;
}
}
}
int ret = dpmap[len1][len2];
// for(int i=0;i<=len1;i++){
// for(int j=0;j<=len2;j++)
// cout<<dpmap[i][j]<<" ";
// cout<<endl;
// }
delete []dpmap[];
delete []dpmap;
return ret;
}
}; int main()
{
string word1 = "";
string word2 = "";
Solution sol;
cout<<sol.minDistance(word1,word2)<<endl;
return ;
}

[LeetCode] Edit Distance 字符串变换为另一字符串动态规划的更多相关文章

  1. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  2. Leetcode:Edit Distance 解题报告

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  3. [leetcode]Edit Distance @ Python

    原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...

  4. Leetcode Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  5. [LeetCode] Edit Distance(很好的DP)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. LeetCode: Edit Distance && 子序列题集

    Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...

  7. LeetCode——Edit Distance

    Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...

  8. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

  9. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

随机推荐

  1. JavaScript中面向对象的三大特性(一个菜鸟的不正经日常)

    经过几天的学习,把jQuery给啃会了,但是运用的还不算特别熟练,总感觉自己在JavaScript方面的基础十分欠缺,所以继续拾起JavaScript,开始更好的编程之旅~ 今天学的是JavaScri ...

  2. STL之stack操作

    c++ stl栈stack介绍 C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构. c++ stl栈stack的头文件 ...

  3. Some tricks

    一 . \(2^i >\sum_{0}^{i - 1}2^i\) 二. 当概率非常小时,且答案允许范围内的误差.如与正确答案不超过\(2^{-6}\)即可. 选取一个较小的值,然后取min即可. ...

  4. 【数学 技巧】divisor

    没考虑重复lcm处理被卡TLE没A真是可惜 题目大意 $n$为$k-可表达的$当且仅当数$n$能被表示成$n$的$k$个因子之和,其中$k$个因子允许相等. 求$[A,B]$之间$k-可表达$的数的个 ...

  5. H5bulider中的微信支付配置注意事项

    一.云打包安卓自定义证书的生成: 签名算法名称: SHA1withRSA主体公共密钥算法:1024 位 RSA 密钥密钥库类型:JKS 1.下载JDK1.6安装,切换到bin目录,打开命令行: 2.生 ...

  6. PHP将html内容转换为image图片

    /** * 将html内容转换为image图片 * @param $htmlcontent * @param $toimagepath * @author james.ou 2011-11-1 */ ...

  7. 【结构型模式】《大话设计模式》——读后感 (12)在NBA我需要翻译?——适配器模式

    适配器模式:将一个类的接口转换成客户希望的另外一个接口,Adapter模式使得原本由于接口不兼容而不能在一起工作的 那些类可以在一起工作了[DP] UML类图: 简单模拟一下代码: //已存在的.具有 ...

  8. LeetCode(304)Range Sum Query 2D - Immutable

    题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  9. POJ:1961-Period(寻找字符串循环节)

    Period Time Limit: 3000MS Memory Limit: 30000K Description For each prefix of a given string S with ...

  10. 图上两点之间的第k最短路径的长度 ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven

    131072K   One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. Howe ...