大意:给定两个字符串word1和word2,为了使word1变为word2,可以进行增加、删除、替换字符三种操作,请输出操作的最少次数
 

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')
 
状态:dp[i][j]把word1[0..i-1]转换到word2[0..j-1]的最少操作次数
状态转移方程:
  (1)如果word1[i-1] == word2[j-1],则令dp[i][j] = dp[i-1][j-1]
  (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
       替换操作:dp[i][j] = dp[i-1][j-1] + 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的更多相关文章

  1. 动态规划 求解 Minimum Edit Distance

    http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...

  2. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  3. Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)

    Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...

  4. Edit Distance——经典的动态规划问题

    题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to conve ...

  5. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  7. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  8. 【leetcode】Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  9. 56. Edit Distance && Simplify Path

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...

随机推荐

  1. Vue+koa2开发一款全栈小程序(9.图书详情页)

    1.获取图书详情 1.修改server/controllers/bookdetail.js为 const {mysql}=require('../qcloud') module.exports=asy ...

  2. SQL随记(二)

    1.purge关键字:可以清除oracle 回收站(recyclebin)中的表和索引并释放与其相关的空间,还可清空回收站,或者清除表空间中记录的已删除的部分表空间.但是purge后不能回滚和恢复. ...

  3. 集合的遍历以及在Spring中的注入

    (一)遍历 list:foreach循环 set:foreach循环 map:先用set集合得到所有key值,然后循环key得到所有vale Map<String,String> map ...

  4. Java WebService接口生成和调用 图文详解>【转】【待调整】

    webservice简介: Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间 ...

  5. SPFA+SLF+LLL

    关于SLF优化 朴素SPFA使用常规队列(FIFO)更新距离,并没有考虑优化出队顺序(dis值小的优先出队)可以在一开始就把各个点的dis值限值小,从而避免大量的松弛操作,从而提高效率.这就是SLF( ...

  6. 使用ArcMap做一个1:5000标准分幅图并编号

    实现这个project,十进制度.渔网工具.Excel if/Text函数.挂接Excel表.空间连接.投影这些知识是必须的.看懂本篇博文也就意味着大概掌握了以上知识. 坐标数据设置与编号标准依据&l ...

  7. ext.net单元格内容换行显示

    增加style .x-grid3-cell-inner {     white-space: normal; }

  8. Linux常用命令大全(非常全!!!)

    Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较短 ...

  9. mac下go环境搭建开发web工程

    1,golang下载: http://www.golangtc.com/download https://golang.org/ https://beego.me/docs/intro/ 2,安装go

  10. webpack构建Vue工程

    先开始webpack基本构建   创建一个工程目录 vue-structure mkdir vue-structure && cd vue-structure   安装webpack ...