[LeetCode] Edit Distance 字符串变换为另一字符串动态规划
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
- 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 字符串变换为另一字符串动态规划的更多相关文章
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode:Edit Distance 解题报告
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [leetcode]Edit Distance @ Python
原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...
- Leetcode Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [LeetCode] Edit Distance(很好的DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode: Edit Distance && 子序列题集
Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...
- LeetCode——Edit Distance
Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
随机推荐
- 数据结构算法与应用c++语言描述 原书第二版 答案(更新中
目录 第一章 C++回顾 函数与参数 1.交换两个整数的不正确代码. 异常 10.抛出并捕捉整型异常. 第一章 C++回顾 函数与参数 1.交换两个整数的不正确代码. //test_1 void sw ...
- 【two pointers 细节题】cf1041dD. Glider
像这样细节老是打挂不行啊…… A plane is flying at a constant height of hh meters above the ground surface. Let's c ...
- 利用wget 和 curl 监控网站是否正常
监控网站URL是否正常最常见的方法莫过于wget和curl命令了,这两个命令都是非常强大,参数也非常多,下面列举几个常用的参数. wget 常用命令参数:--spider ...
- matplotlib学习记录 三
# 绘制自己和朋友在各个年龄的女友数量的折线图 from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.rcParams['font. ...
- 搭建本地虚拟服务器linux(CentOS 7)的python虚拟环境(Hyper-V演示)
新建虚拟机->安装CentOS7->新建虚拟交换机:内部网络->CentOS7设置->网络适配器:虚拟交换机:新建虚拟交换机->进入CentOS # cd /etc/sy ...
- 学习ucosii要用到的几本书
转自:http://bbs.elecfans.com/jishu_551275_1_1.html 1.嵌入式实时操作系统μC/OS-II(第2版) 邵贝贝 等译 北京航空航天大学出版社 ...
- centos配置jdk
########## config jdk ########## export JAVA_HOME=/usr/local/java/jdk1.7.0_79 export CLASSPATH=.:${J ...
- Linux学习-Linux 主机上的用户讯息传递
查询使用者: w, who, last, lastlog 如果你想要知道目前已登入在系统上面的用户呢?可以透过 w 或 who 来查询喔!如下范例所示: [root@study ~]# w 01:49 ...
- BZOJ 5215: [Lydsy2017省队十连测]商店购物
裸题 注意+特判 #include<cstdio> using namespace std; const int mod=1e9+7; int F[1000005],mi[10000005 ...
- css 阴影使用
文本阴影 p{ text-shadow: 5px 5px 5px #FF0000; } text-shadow: h-shadow v-shadow blur color; text-shadow: ...