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 ...
随机推荐
- copy和mutableCopy的深、浅拷贝
对象拷贝主要由两种方式:copy和mutableCopy.浅拷贝是指直接将指针指向原有的地址,从而达到复制的目的.深拷贝是指重新生成一个对象,将原有对象的内容复制到新的对象中.copy 返回的是一个不 ...
- WCF 无法生成 client
在MVC中调用WCF 总是没有client 后来在网上查找原因,去掉Reuse type in referrenced assenbiles ,就可以生成代理代码.
- 2014-11-9------- 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- 在ASP.NET 中调用RSACryptoServiceProvider失败,提示未找到文件
在本地开发环境调试下,调试这个RSA加密是没问题的,但是部署到IIS就会报错. 原来是,在本地vs调试与IIS上运行是存在权限差异的.本地调试权限最大,IIS 次之. 所以在我们声明CspParame ...
- Java 设计模式_代理模式(2016-08-19)
概念: 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. 就是一个人或者机构代表另一个人或者机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一 ...
- linux系统 web在线日志分析
线上环境出现问题时,不能像本地环境一样,断点查找问题,只有根据日志分析来定位问题,当然有资深的经验也是可以的,哈哈. 最基本的就是cat命令,可以通过cat filename,来查看文件全部内容, & ...
- boost::thread boost库线程
一.boost::thread的创建 1.线程创建方法一: boost::shared_ptr<boost::thread> writeThread_; boost::function0& ...
- Problem 1010 - 素数环问题
#include<iostream> #include<string> #include<algorithm> #include<vector> #in ...
- Microsoft Visual Studio 2010 遇到了异常,可能是由某个扩展导致的。 转载
问题: 今天打开好久没用的Microsoft Visual Studio 2010 ,刚才创建了一个C++工程,错误就出现了. 只要在VS2010源码编辑器中输入一个字符,它就报错 ":Mi ...
- PDF在xp或2003下正常在win7下乱码的问题
1.先确定当前PDF文件需要字体(在PDF工具打开找到Font字体可以查看具体需要哪些字体). 2.网上下载或者在生成PDF的电脑上把老版本字体拷贝出来然后在win7下安装,当提示已经存在该字体时,直 ...