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。

我写的如下:

  1. #include <iostream>
  2. #include <string>
  3. #include <memory.h>
  4. using namespace std;
  5.  
  6. class Solution {
  7. public:
  8. int minDistance(string word1, string word2) {
  9. int len1 = word1.length(),len2=word2.length();
  10. if(len1==) return len2;
  11. if(len2==) return len1;
  12. int **dpmap = new int *[len1+];
  13. dpmap[] =new int[(len1+)*(len2+)];
  14. memset(dpmap[],,sizeof(int)*(len1+)*(len2+));
  15. for(int i= ;i<=len1;i++)
  16. dpmap[i] = dpmap[i-]+len2+;
  17. for(int i=;i<=len1;i++)
  18. dpmap[i][] = i;
  19. for(int j=;j<=len2;j++)
  20. dpmap[][j] = j;
  21. for(int i=;i<=len1;i++){
  22. for(int j=;j<=len2;j++){
  23. if(word1[i-]==word2[j-]) dpmap[i][j]=dpmap[i-][j-];
  24. else{
  25. dpmap[i][j]=(dpmap[i-][j]>dpmap[i][j-]?dpmap[i][j-]:dpmap[i-][j])+;
  26. if(dpmap[i-][j-]+<dpmap[i][j])
  27. dpmap[i][j] = dpmap[i-][j-]+;
  28. }
  29. }
  30. }
  31. int ret = dpmap[len1][len2];
  32. // for(int i=0;i<=len1;i++){
  33. // for(int j=0;j<=len2;j++)
  34. // cout<<dpmap[i][j]<<" ";
  35. // cout<<endl;
  36. // }
  37. delete []dpmap[];
  38. delete []dpmap;
  39. return ret;
  40. }
  41. };
  42.  
  43. int main()
  44. {
  45. string word1 = "";
  46. string word2 = "";
  47. Solution sol;
  48. cout<<sol.minDistance(word1,word2)<<endl;
  49. return ;
  50. }

[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. 数据结构算法与应用c++语言描述 原书第二版 答案(更新中

    目录 第一章 C++回顾 函数与参数 1.交换两个整数的不正确代码. 异常 10.抛出并捕捉整型异常. 第一章 C++回顾 函数与参数 1.交换两个整数的不正确代码. //test_1 void sw ...

  2. 【two pointers 细节题】cf1041dD. Glider

    像这样细节老是打挂不行啊…… A plane is flying at a constant height of hh meters above the ground surface. Let's c ...

  3. 利用wget 和 curl 监控网站是否正常

    监控网站URL是否正常最常见的方法莫过于wget和curl命令了,这两个命令都是非常强大,参数也非常多,下面列举几个常用的参数. wget  常用命令参数:--spider              ...

  4. matplotlib学习记录 三

    # 绘制自己和朋友在各个年龄的女友数量的折线图 from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.rcParams['font. ...

  5. 搭建本地虚拟服务器linux(CentOS 7)的python虚拟环境(Hyper-V演示)

    新建虚拟机->安装CentOS7->新建虚拟交换机:内部网络->CentOS7设置->网络适配器:虚拟交换机:新建虚拟交换机->进入CentOS # cd /etc/sy ...

  6. 学习ucosii要用到的几本书

    转自:http://bbs.elecfans.com/jishu_551275_1_1.html   1.嵌入式实时操作系统μC/OS-II(第2版)  邵贝贝 等译 北京航空航天大学出版社     ...

  7. centos配置jdk

    ########## config jdk ########## export JAVA_HOME=/usr/local/java/jdk1.7.0_79 export CLASSPATH=.:${J ...

  8. Linux学习-Linux 主机上的用户讯息传递

    查询使用者: w, who, last, lastlog 如果你想要知道目前已登入在系统上面的用户呢?可以透过 w 或 who 来查询喔!如下范例所示: [root@study ~]# w 01:49 ...

  9. BZOJ 5215: [Lydsy2017省队十连测]商店购物

    裸题 注意+特判 #include<cstdio> using namespace std; const int mod=1e9+7; int F[1000005],mi[10000005 ...

  10. css 阴影使用

    文本阴影 p{ text-shadow: 5px 5px 5px #FF0000; } text-shadow: h-shadow v-shadow blur color; text-shadow: ...