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 ...
随机推荐
- C#管理异常和错误
C#管理异常和错误 1.try/catch捕捉异常的语句块,其中try{}中是写可能会出错的程序代码,catch{}中是抛出异常的代码:一个try后可以有多个catch. 2.异常采用继承层次结构进行 ...
- jQuery分析(2) - $工厂函数分析
前言 从这节进入jQuery的世界,首先从jQuery的入口函数开始了解jQuery()或$是如何运作的,这里我给出了一个最小的例子来分析. 回忆 在进入分析代码前我们回想下jQuery的使用方法有哪 ...
- obj.onclick=fnClick与obj.onclick=fnClick()的区别
先说结论:这段代码浏览器会报错,提示 aDiv[this.index] is undefined 所以正确的写法应该是去掉(),直接写为function fnClick. 不加括号的话,相当于指定 ...
- [转载] CMake Official Tutorial——教程还是官方的好
CMake官方教程传送门:https://cmake.org/cmake-tutorial/ 以下的内容跟官方教程基本一致,少数地方根据自己的测试有所改动: A Basic Starting Poin ...
- 适合自己的vim配置文件
主要用来写c++的:clang-completer这个是单独安装的,其他的都采用的vundle安装完成. clang-completer:只在centos7.2上安装成功过,6.4上失败了.先要安装一 ...
- 找不好重现的bug的一个小技巧————守株待兔
最近碰到一个问题就是数据库中偶尔出现一条没有id的数据,可恨的是怎么也找不到重现这个问题的原因,只好换种方式来找了,那么就是我标题所说的守株待兔方法. 因为我发现出现bug的数据库里面的数据有个字段为 ...
- Codeforces 553D Nudist Beach(图论,贪心)
Solution: 假设已经选了所有的点. 如果从中删掉一个点,那么其它所有点的分值只可能减少或者不变. 如果要使若干步删除后最小的分值变大,那么删掉的点集中肯定要包含当前分值最小的点. 所以每次删掉 ...
- c++预编译问题:fatal error C1083: Cannot open precompiled header file: 'Debug/DllTest.pch': No such file or d
1)单独编译StdAfx.cpp 2)编译所有(即按Ctrl+F7) 这时因为该模块没有包括预编译头文件“stdafx.h”的缘故.VC用一个stdafx.cpp包含头文件stdafx.h,然后在st ...
- [学习笔记]设计模式之Decorator
写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 Decorator(装饰)模式,可以动态地给一个对象添加一些额外的职能.为了更好地理解这个模式,我们将时间线拉回Bridge模式笔记的 ...
- Java连接mySql—JDBC连接数据库
利用JDBC开发数据库 经典应该用框架: 第一步,加载JDBC数据库驱动程序(不同的数据库有不同的数据库驱动,所以在连接数据库之前,需加载驱动) 格式: String driver = "c ...