动态规划——Edit Distance
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Example 2:
Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
状态转移方程:
(2)如果word1[i-1] != word2[j-1],由于没有一个特别有规律的方法来断定执行何种操作,在增加、删除、替换三种操作中选一种操作次数少的赋值给dp[i][j];
增加操作:dp[i][j] = dp[i][j-1] + 1
删除操作:dp[i][j] = dp[i-1][j] + 1
int minDistance(string word1,string word2){
int wlen1 = word1.size();
int wlen2 = word2.size(); int**dp = new int*[wlen1 + ];
for (int i = ; i <= wlen1; i++)
dp[i] = new int[wlen2 + ]; //int dp[maxn][maxn] = { 0 };
for (int i = ; i <= wlen1; i++)
dp[i][] = i;
for (int j = ; j <= wlen2; j++)
dp[][j] = j;
int temp = ;
for (int i = ; i <= wlen1; i++){
for (int j = ; j <= wlen2; j++){
if (word1[i - ] == word2[j - ])dp[i][j] = dp[i - ][j-];
else{
temp = dp[i - ][j - ]<dp[i - ][j] ? dp[i - ][j - ] : dp[i - ][j];
temp = temp < dp[i][j - ] ? temp : dp[i][j - ];
dp[i][j] = temp + ;
}
}
} /*
for (int i = 0; i <= wlen1; i++)
delete[]dp[i];
delete[]dp;
*/ return dp[wlen1][wlen2];
}
动态规划——Edit Distance的更多相关文章
- 动态规划 求解 Minimum Edit Distance
http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)
Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...
- Edit Distance——经典的动态规划问题
题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to conve ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- WS_TABSTOP 与 BS_DEFPUSHBUTTON 第一次使有
关于 WS_TABSTOP,我一直没搞明白,为何加了 WS_TABSTOP ,当按下 Tab 键时,窗口中的控件还是不能自动的切换焦点. 为何 SetFocus(hwnd); 后,虽然按钮已获得焦点, ...
- qml : qml控件自适应;
import QtQuick 2.4 Item { property var targetItem: parent property bool fixedAspectRatio: true // El ...
- Mysql加锁过程详解(9)-innodb下的记录锁,间隙锁,next-key锁
Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...
- Blender 2.78 突然卡顿
之前一直好好的直到这两天打开就一直延迟半拍,重启重装无效. 解决方法: NVIDIA控制面板→管理3d设置→程序设置→选择blender,更改显卡为集成显卡→应用 亲测有效.
- Java(4)switch选择结构
一.switch结构(开关语句)的语法 switch(表达式 ){--->类型为int.char case 常量1 :--->case 结构可以有多个 //语句块1 break;---& ...
- topshelf 服务启动运行
topshlf 服务启动运行方式 安装:servicename.exe install 启动:servicename.exe start 卸载:servicename.exe uninstall
- gulp学习笔记——最好的学习文档是官网
官网:http://www.gulpjs.com.cn/docs/api/ 当然还有一个博客写的也很好,当我看不下去官网的时候,这个帮助了我很多,明了易懂:http://www.ydcss.com/a ...
- Ubuntu18.04更换官方默认更新源sources.list
⒈备份官方默认更新源文件 cp /etc/apt/sources.list /etc/apt/sources.list.bak 备份官方更新源文件 ⒉编辑 1.打开 vi /etc/apt/sourc ...
- 【原创】大数据基础之Alluxio(1)简介、安装、使用
Alluxio 1.8.1 官方:http://www.alluxio.org/ 一 简介 Open Source Memory Speed Virtual Distributed StorageAl ...
- 【原创】运维基础之yum离线环境安装软件
首先查看系统版本号,然后根据版本号从 CentOS-7-x86_64-DVD-1708.iso 和 CentOS-7-x86_64-Everything-1708.iso 根据需要选择一个下载,我这里 ...