[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 ...
随机推荐
- 测试 code style
c++ #include <iostream> int main(int argc, char *argv[]) { /* An annoying "Hello World&qu ...
- Visual Studio 2017 UTF-8 无 BOM 一站式解决办法
问题背景:最近捡起C++,使用VS 2017平台.因为以前的编程习惯,喜欢使用UTF-8 无 BOM 的编码格式,好让自己的代码全球通用.但是VS 2017 对这个问题不是很友善.但最终找到了解决办法 ...
- 【网络基础】【TCP/IP】IP的分级
节选自 <鸟哥的linux私房菜> http://cn.linux.vbird.org/linux_server/0110network_basic_3.php#ps12 InterNI ...
- python3 完全平方数(循环)
题目 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 代码: for i in range(1,85): if 168 % i == 0: j = 168 ...
- selenium+phantomjs爬取京东商品信息
selenium+phantomjs爬取京东商品信息 今天自己实战写了个爬取京东商品信息,和上一篇的思路一样,附上链接:https://www.cnblogs.com/cany/p/10897618. ...
- sql执行过长,如何入手优化
一条sql执行过长的时间,你如何优化,从哪些方面 1.查看sql是否涉及多表的联表或者子查询,如果有,看是否能进行业务拆分,相关字段冗余或者合并成临时表(业务和算法的优化)2.涉及链表的查询,是否能进 ...
- Django之FileField字段
头像上传 在头像上传的时候,属于文件类型 首先视图函数获取的时候,request.FILES.get('文件名变量') avatar_obj = request.FILES.get('avatar') ...
- pycharm-install scipy
懒得装双系统,所以在win7下用pycharm,python2.7 虽然机子本身是64位,但是安装包的时候,我居然需要下载32位的??迷:) 这次装的是scipy.在pycharm里添加不了,根据网上 ...
- python3与python2的编码问题
在讲这个问题之前,我们先说说unicode的工作原理.unicode包含了跟全球所有国家编码的映射关系,就是不管你用哪个国家的编码,unicode都能找到它在unicode中的编码.那么无论你用什么编 ...
- Virtual Friends HDU - 3172 (并查集+秩+map)
These days, you can do all sorts of things online. For example, you can use various websites to make ...