[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 ...
随机推荐
- iOS 通过storyboard设置UIView或者其他layer图层的圆角
通常我们给Button或者UIView添加圆角是通过如下代码进行实现的 self.button.layer.cornerRadius=10; 但是如果你是使用的故事版或者xib进行设计视图的话,实际上 ...
- 从 Objective-C 里的 Alloc 和 AllocWithZone 谈起
一.问题起源 一切起源于Apple官方文档里面关于单例(Singleton)的示范代码:Creating a Singleton Instance.主要的争议集中在下面这一段: static MyGi ...
- 【差分约束】poj1275Cashier Employment
比较经典的差分约束 Description A supermarket in Tehran is open 24 hours a day every day and needs a number of ...
- [51nod] 1301 集合异或和
考虑不限制xor{Y}>xor{X} 考虑n=m的情况,每个数i∈[1,n]可以被分配到X集合或Y集合,或不分配 设f[S]表示{X} xor {Y} == S的方案数 有f[S]+=2*f[S ...
- 拓扑排序 topsort
拓扑排序 对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序 ...
- JAVA中文字符串编码--GBK转UTF-8
转载自:https://www.cnblogs.com/yoyotl/p/5979200.html 一.乱码的原因 gbk的中文编码是一个汉字用[2]个字节表示,例如汉字“内部”的gbk编码16进制的 ...
- python入门:数字型和字符串换行要同类型 注意连接符
#!/usr/bin/env python # -*- coding: utf-8 -*- #数字型和字符串换行要同类型 注意连接符 a = 1 b = 2 print(str(a) + " ...
- python爬虫基础18-Chrome调试前端工具
01 Chrome调试 抓包工具原理 Chrome 开发者工具是一套内置在Google Chrome中Web开发和调试工具.使用开发者工具来重演,调试和剖析您的网站. 其中常用的有Elements(元 ...
- python中字符串的一些用法
一.字符串的拼接: a=‘123’ b=‘abc’ d=‘hello world’ 1.print(a+b) 2.print(a,b) 3. c=‘ ’.join((a ...
- PAT Basic 1074
1074 宇宙无敌加法器 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每个 PAT 星 ...