编辑距离

问题描述 :
给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符

示例 1:
输入:word1 = “horse”, word2 = “ros”
输出:3
解释:
horse -> rorse (将 ‘h’ 替换为 ‘r’)
rorse -> rose (删除 ‘r’)
rose -> ros (删除 ‘e’)

示例 2:
输入:word1 = “intention”, word2 = “execution”
输出:5
解释:
intention -> inention (删除 ‘t’)
inention -> enention (将 ‘i’ 替换为 ‘e’)
enention -> exention (将 ‘n’ 替换为 ‘x’)
exention -> exection (将 ‘n’ 替换为 ‘c’)
exection -> execution (插入 ‘u’)

输入说明 :
输入两行,第一行为第一个单词word1,第二行为第二个单词word2

输出说明 :
输出结果

输入范例 :
horse
ros

输出范例 :
3

#include<iostream>
#include<vector>
using namespace std;

int minDistance(string word1,string word2){
	int m = word1.length();
        int n = word2.length();
        
        vector<vector<int>> cost(m+1, vector<int>(n+1));
        
        for (int i = 0; i <= m; ++i) {
            cost[i][0] = i;
        }
        for (int j = 0; j <= n; ++j) {
            cost[0][j] = j;
        }
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (word1[i-1] == word2[j-1]) {
                    cost[i][j] = cost[i-1][j-1];
                } else {
                    cost[i][j] = 1 + min(cost[i-1][j-1], min(cost[i][j-1], cost[i-1][j]));
                }             
            }
        }
        return cost[m][n];
}

int main(){
	string word1,word2;
	cin>>word1;
	cin>>word2;
	int res=minDistance(word1,word2);
	cout<<res;
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编辑距离是指两个字符串之间,通过一系列的操作(删除、插入、替换)将一个字符串转换成另一个字符串所需的最小次数。在Python中,有多种方法可以计算编辑距离。 一种方法是使用第三方库Levenshtein,通过调用Levenshtein.distance(str1, str2)方法来计算编辑距离。这个方法使用了优化的算法结构,内部调用了C库,因此执行速度比自己编写的代码更快。 另一种方法是使用动态规划(DP)算法,可以通过编写自己的代码来计算编辑距离。下面是一个简单的使用Python的列表实现的例子: ```python def edit_distance(str1, str2): matrix = [[i + j for j in range(len(str2) + 1)] for i in range(len(str1) + 1)] for i in range(1, len(str1) + 1): for j in range(1, len(str2) + 1): if str1[i - 1 == str2[j - 1]: d = 0 else: d = 1 matrix[i][j = min(matrix[i - 1][j + 1, matrix[i][j - 1 + 1, matrix[i - 1][j - 1 + d) return matrix[len(str1)][len(str2)] ``` 这个算法使用了一个二维矩阵来存储中间结果,通过动态规划的方式逐步计算出最小编辑次数。 编辑距离是一个常用的字符串相似性度量方法,它可以用于文本纠错、DNA序列比对等领域。它的应用很广泛,可以在自然语言处理(NLP)任务中起到重要的作用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [编辑距离算法详解和python代码](https://blog.csdn.net/weixin_41665541/article/details/84942196)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值