⭐【编辑距离问题】并通过回溯输出编辑过程

题目描述:

给定一个源串和目标串,能够对源串进行如下操作:
在任意位置上插入一个字符;
替换任意字符;
删除任意字符。
写一个程序,实现返回最小操作次数,使得对源串进行上述这些操作后等于目标串(源串和目标串的长度都小于2000)。


解析:

拿到编辑距离问题,最朴素的想法便是暴力遍历所有的解决方案,即用DFS或者BFS+优先队列来存每次遍历的结果,最后找到最优解。显然,这种思路可行性低,算法的时间、空间复杂度较高。经过思考,我发现编辑距离问题的最优解中包含了子问题的最优解,且其独立的子问题的解被重复计算了多次。因此可以将编辑距离问题划分为若干个规模更小的子问题,这符合动态规划的特征及基本思想,考虑用动态规划解决。
首先定义这样一个数组——dp[len1+1][len2+1],其中len1len2分别代表输入的源字符串和目标字符串的长度。dp[i][j]代表源字符串的前i个字符转换为目标字符串的前j个字符所需的最少操作步数。

经过归纳推导得出动态规划状态转移方程:


设计:

  1. 动态规划求最小操作数(寻找最优解):
    核心思路为初始化dp表+套用状态转移方程;
    在这里插入图片描述

  2. 回溯法输出转换过程:
    从结果dp[len1][len2]开始自底向上回溯,共有四个分支。分别为dp[i-1][j]+1dp[i][j-1]+1dp[i-1][j-1]+1dp[i-1][j-1]dp[i][j]的值做比对。
    由于回溯结果是倒着的,所以在实际代码中用了一个文本流进行倒序存储回溯结果,输出后就是正序的了。
    在这里插入图片描述


分析:

m=len(source_str); n=len(target_str);

  1. 时间复杂度分析:
    在动态规划建立dp表时,两层for循环,时间复杂度为O(mn);
    在回溯时,一层while循环,时间复杂度为O(max{m,n})
    因此,本程序总时间复杂度为O(mn);
  2. 空间复杂度分析:
    开辟了二维数组dp[m][n],空间复杂度为O(mn);

题解:

#include <bits/stdc++.h>
using namespace std;
int main(){
	string source_str, target_str;
	stringstream line;	// 记输出的第一行的内容,避免多做一次循环 
	cout<<"源字符串: "; cin>>source_str;
	cout<<"目标字符串: "; cin>>target_str;
	int len1 = source_str.length(), len2 = target_str.length();
	int dp[len1+1][len2+1];	// dp[i][j]代表source_str前i个字符 →target_str前j个字符所需的最少步数 
	cout<<"--------------------------------------------"<<endl<<" ";
	for(int j = 0; j <= len2; ++j) {	// 初始化dp数组,并输出表头 
		dp[0][j] = j;
		if(j == 0) cout<<" 空 ";
		else cout<<target_str[j-1]<<" ";
		line<<j<<" ";
	} 
	cout<<endl<<"空 "<<line.str();
	for(int i = 0; i <= len1; ++i) {	// 初始化dp数组,并输出表头
		dp[i][0] = i;
	} 
	cout<<endl;
	// 核心代码
	for(int i = 1; i <= len1; ++i) {
		cout<<" "<<source_str[i-1]<<" "<<i<<" ";
		for(int j = 1; j <= len2; ++j) {
			if(source_str[i-1] == target_str[j-1]){		// 当前的字符相等则dp[i][j]=dp[i-1][j-1] 
				dp[i][j] = dp[i-1][j-1];
			}
			else{	// 1.替换:d[i][j]=d[i-1][j-1]+1; 2.target_str的字母插入source_str:d[i][j] = d[i][j-1] + 1; 3.删除source_str的字母:d[i][j] = d[i-1][j] + 1;
				dp[i][j] = 1 + min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]));
			}
			cout<<dp[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<"--------------------------------------------"<<endl<<"最小操作次数为 "<<dp[len1][len2]<<endl<<endl;
	cout<<"---------------转换过程回溯----------------"<<endl;
	int i = len1, j = len2 ;
	int mindis = dp[i][j];
	string performs = "";
	while(i > 0 && j > 0) {
		if(dp[i][j-1]+1 == dp[i][j]) {			// mindis = d[i][j-1];
			stringstream perform;
			perform<<"源字符串 插入字符 "<<target_str[j-1]<<" , 操作代价为1;"<<endl;
			performs = perform.str() + performs;
			j--;
		} 
		else if(dp[i-1][j]+1 == dp[i][j]) {		// mindis = d[i-1][j];
			stringstream perform;
			perform<<"目标字符串 删除字符 "<<source_str[i-1]<<" , 操作代价为1;"<<endl;
			performs = perform.str() + performs;
			i--;
		} 
		else if(dp[i-1][j-1]+1 == dp[i][j]) {	// mindis = d[i-1][j-1];
			stringstream perform;
			perform<<"源字符串 中的 "<<source_str[i-1]<<" 替换为 目标字符串 的 "<<target_str[j-1]<<" , 操作代价为1;"<<endl;
			performs = perform.str() + performs;
			i--; j--;
		} 
		else if(dp[i-1][j-1] == dp[i][j]) {		// d[i][j] = d[i-1][j-1];
			stringstream perform;
			perform<<"源字符串 中的 "<<source_str[i-1]<<" 匹配 目标字符串 的 "<<target_str[j-1]<<" , 操作代价为0;"<<endl;
			performs = perform.str() + performs;
			i--; j--;
		} 
		else {
			i--; j--;
		}
	}
	cout<<performs;
	return 0;
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编辑距离问题是一个经典的动态规划问题,可以使用DP算法来解决。下面给出一个C++的代码实现,同时输出具体的编辑方法。 ```C++ #include <iostream> #include <string> #include <vector> using namespace std; int min(int a, int b, int c) { return min(min(a, b), c); } void printEditMethod(vector<vector<int>>& dp, string s1, string s2) { int m = s1.size(), n = s2.size(); vector<string> method; while (m > 0 && n > 0) { if (s1[m - 1] == s2[n - 1]) { method.push_back("no edit"); m--; n--; } else if (dp[m][n] == dp[m - 1][n - 1] + 1) { method.push_back("replace " + string(1, s2[n - 1])); m--; n--; } else if (dp[m][n] == dp[m - 1][n] + 1) { method.push_back("delete " + string(1, s1[m - 1])); m--; } else if (dp[m][n] == dp[m][n - 1] + 1) { method.push_back("insert " + string(1, s2[n - 1])); n--; } } while (m > 0) { method.push_back("delete " + string(1, s1[m - 1])); m--; } while (n > 0) { method.push_back("insert " + string(1, s2[n - 1])); n--; } for (int i = method.size() - 1; i >= 0; i--) { cout << method[i] << endl; } } int editDistance(string s1, string s2) { int m = s1.size(), n = s2.size(); vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); for (int i = 0; i <= m; i++) { dp[i][0] = i; } for (int j = 0; j <= n; j++) { dp[0][j] = j; } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1; } } } printEditMethod(dp, s1, s2); return dp[m][n]; } int main() { string s1 = "kitten"; string s2 = "sitting"; int distance = editDistance(s1, s2); cout << "The edit distance between " << s1 << " and " << s2 << " is " << distance << endl; return 0; } ``` 在输出编辑距离的同时,我们定义了一个`printEditMethod`函数,用于输出具体的编辑方法。该函数从`dp`表右下角开始,根据当前格子的值和相邻格子的值来确定编辑方法,直到达到左上角。最后,将方法按照逆序输出即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值