leetcode72. 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
编辑距离是算法导论的一道作业题,不过leetcode的这道题比较简单,权重都一样。
令dp[i][j]代表word1[0..i]和word2[0..j]的编辑距离
则当word1[i]==word[j]时,dp[i][j]=dp[i-1][j-1]
word1[i]!=word[j]时,dp[i][j] = min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1
class Solution {
public:
inline bool find(const string &str,char &ch)
{
for(char c:str)
{
if(c==ch)
return true;
}
return false;
}
inline int min(int a,int b,int c)
{
if(a<=b &&a<=c)
return a;
if(b<=a &&b<=c)
return b;
else return c;
}
int minDistance(string word1, string word2) {
int row = word1.length();
int col = word2.length();
if(row== || col==) return row+col;
vector<vector<int>> dp(row,vector<int>(col,));//dp[i][j]代表word1[0..i]和word2[0..j]的编辑距离
//先确定第一行和第一列
for(int i=;i<col;i++)
{
if(find(word2.substr(,i+),word1[]))
dp[][i] = i;
else
dp[][i] = i+;
}
for(int i=;i<row;i++)
{
if(find(word1.substr(,i+),word2[]))
dp[i][] = i;
else
dp[i][] = i+;
}
for(int i=;i<row;i++)
{
for(int j=;j<col;j++)
{
if(word1[i] == word2[j])
dp[i][j] = dp[i-][j-];
else
dp[i][j] = min(dp[i-][j],dp[i][j-],dp[i-][j-])+;
}
}
return dp[row-][col-];
}
};
leetcode72. Edit Distance的更多相关文章
- leetcode72. Edit Distance(编辑距离)
以下为个人翻译方便理解 编辑距离问题是一个经典的动态规划问题.首先定义dp[i][j表示word1[0..i-1]到word2[0..j-1]的最小操作数(即编辑距离). 状态转换方程有两种情况:边界 ...
- [leetcode72]Edit Distance(dp)
题目链接:https://leetcode.com/problems/edit-distance/ 题意:求字符串的最短编辑距离,就是有三个操作,插入一个字符.删除一个字符.修改一个字符,最终让两个字 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [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 ...
- LintCode Edit Distance
LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...
- stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)
I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
随机推荐
- Jquery全选与反选checkbox(代码示例)
今天在公司要实现操作权限的功能,需要根据左边的树,选择一项,判断右边的操作权限,例如,增加,修改,删除,查看等按钮的显示与隐藏: 这个功能实现思路如下: 1.操作权限:增加.修改等按钮的ID和Text ...
- DataGridView 绑定 List
DataGridView 绑定 List<T> 不会自动更新 正确方式是将 List<T> 设置为 BindingList<T> 即可 (双向绑定)
- while循环的跳出
今天在编码时突然产生一个疑问:程序中有一个while循环,循环体执行的是某个附带条件限制的操作.我现在想达到的目的是 => 条件成立,就执行操作,并跳出循环:条件不成立就跳出当次的while循环 ...
- Angularjs总结(五)指令运用及常用控件的赋值操作
1.常用指令 <div ng-controller="jsyd-controller"> <div style="float:left;width:10 ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...
- 3D math primer for graphics and game development
三角网格(Triangle Mesh) 最简单的情形,多边形网格不过是一个多边形列表:三角网格就是全部由三角形组成的多边形网格.多边形和三角网格在图形学和建模中广泛使用,用来模拟复杂物体的表面,如建筑 ...
- Touch组件实现原理
Touch组件的实现主要解决了在pc端和移动端拖拽元素的功能. PC端: 依靠事件: mousedown,mousemove,mouseup的鼠标事件.过程: 1. mousedown事件中记录当前元 ...
- MyEclipse使用经验总结
0.快捷键 ================================================================================ 编辑: Ctrl+Shif ...
- baba 运动网
import com.sun.image.codec.jpeg.* 找不到包 在Myeclipse中编译项目时,如果提示类似 com.sun.image.codec.jpeg.* 下: imp ...
- Welcome to JimmyCheung's blog!
博客开通,写写学习笔记,写写心情,写写生活中的点点滴滴~ 有钱的捧个钱场嘞,没钱的贡献个点击量,新鲜的博客出炉咯,来五毛钱的博文呗~ By Jimmy 2014.09.16