编辑距离(动态规划)


public class 编辑距离 {
	private static char str1[] = "Male".toCharArray();
	private static char str2[] = "Female".toCharArray();
	private static char str3[] = "Male".toCharArray();
	private static char str4[] = "Man".toCharArray();
	private static char str5[] = "Adult".toCharArray();
	private static char str6[] = "Male".toCharArray();
	public static int min(int a, int b) {
		return (a > b ? b : a);
	}

	public static int minval(int a, int b, int c) {
		int zhi = min(a, b);
		int minvalue = min(zhi, c);
		return minvalue;
	}

	void editDistance(char[] str1, char[] str2) {
		int lenthofstr1 = str1.length;
		int lenthofstr2 = str2.length;
		int distance[][] = new int[lenthofstr1 + 1][lenthofstr2 + 1];
		// 变量用来遍历、初始化字符串
		int i, j;

		for (i = 0; i < lenthofstr1 + 1; i++) {
			distance[i][0] = i;
		}
		for (i = 0; i < lenthofstr2 + 1; i++) {
			distance[0][i] = i;
		}
		for (i = 1; i < lenthofstr1 + 1; i++) {
			for (j = 1; j < lenthofstr2 + 1; j++) {
				// 如果对应的字符相等,原问题交给子问题处理,即不用任何操作
				if (str1[i - 1] == str2[j - 1]) {
					distance[i][j] = distance[i - 1][j - 1];
				} else {
					// 否则的话,对左、右、左上角的值进行求最小值
					distance[i][j] = minval(distance[i - 1][j] + 1, distance[i][j - 1] + 1, distance[i - 1][j - 1] + 1);
				}
				System.out.print(distance[i][j] + " ");
			}
			System.out.println();
		}
		System.out.println("最少的操作次数是:" + distance[lenthofstr1][lenthofstr2]);
		System.out.println("d(string1,string2)=" + distance[lenthofstr1][lenthofstr2]);
	}

	public static void main(String[] args) {
		编辑距离 a=new 编辑距离();
		a.editDistance(str1, str2);
		a.editDistance(str3, str4);
		a.editDistance(str5, str6);
	}

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编辑距离动态规划公式可以表示为以下递推关系: 如果字符串a的第i个字符等于字符串b的第j个字符,则编辑距离dp[i][j]等于dp[i-1][j-1],即不进行任何操作。 如果字符串a的第i个字符不等于字符串b的第j个字符,则编辑距离dp[i][j]等于以下三种操作的最小值: 1. 替换操作:将字符串a的第i个字符替换为字符串b的第j个字符,此时编辑距离为dp[i-1][j-1]+1。 2. 删除操作:将字符串a的第i个字符删除,此时编辑距离为dp[i-1][j]+1。 3. 插入操作:在字符串a的第i个字符后插入字符串b的第j个字符,此时编辑距离为dp[i][j-1]+1。 因此,综合以上分析,编辑距离动态规划公式可以表示为: 如果字符串a的第i个字符等于字符串b的第j个字符,则dp[i][j]=dp[i-1][j-1]; 如果字符串a的第i个字符不等于字符串b的第j个字符,则dp[i][j]=min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+1)。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [动态规划---编辑距离](https://blog.csdn.net/m0_51431003/article/details/127149925)[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_1"}}] [.reference_item style="max-width: 50%"] - *2* [基于动态规划思想的编辑距离计算](https://download.csdn.net/download/weixin_38502510/13754811)[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_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值