计算机算法--动态规划计算编辑距离

1 Edit-Distance

示例输入(两个字符串,可包含空格):

She smiled and left, and the music changed to a Billy Joel tune.

We came to a stop and stood in the silent forest, listening.

示例输出(返回编辑距离值)

47

字符串A通过插入、删除、替换字符变成另一个字符串B,操作的次数表示两个字符串的差异。

用于计算文本相关性、相似性

递归关系

两字符串长度为NM,对1≤i≤N1≤j≤M,有

ai=bj LD(i,j)=LD(i-1,j-1)

ai≠bj LD(i,j)=Min(LD(i-1,j-1),LD(i-1,j),LD(i,j-1))+1

求解例

A=GGATCGAB=GAATTCAGTTA,计算LD(A,B)

LD算法矩阵

G

A

A

T

T

C

A

G

T

T

A

0

1

2

3

4

5

6

7

8

9

10

11

G

1

G

2

A

3

T

4

C

5

G

6

A

7

 

 

import java.util.Scanner;

public class niuqingshan {
	
		/**
		 * @param args
		 */
		public static void main(String[] args) {
			/*Scanner in=new Scanner(System.in);
			String A=in.nextLine();
			Scanner in1=new Scanner(System.in);
			String B=in1.nextLine();*/
			String A="She smiled and left, and the music changed to a Billy Joel tune.";
			String B="We came to a stop and stood in the silent forest, listening.";
			System.out.println(LDfuction(A,B));

		}
		private static int Min(int i,int j,int k)
		{
			int min=0;
			if(i<=j)min=i;
			if(i>=k)min=k;
			return min;
		}
		public static int LDfuction(String strA,String strB )
		{
			char[] CharArrA=strA.toCharArray();
			char[] CharArrB=strB.toCharArray();
			int lengthA=CharArrA.length+1;
			int lengthB=CharArrB.length+1;
			int[][]LD=new int[lengthA][lengthB];
			for(int i=0;i<lengthA;i++)
				LD[i][0]=i;
			for(int j=0;j<lengthB;j++)
				LD[0][j]=j;
			for(int i=1;i<lengthA;i++)
				for(int j=1;j<lengthB;j++)
				{
					if(CharArrA[i-1]==CharArrB[j-1])
						LD[i][j]=LD[i-1][j-1];
					else{
						
						LD[i][j]=Min(LD[i-1][j-1],LD[i-1][j],LD[i][j-1])+1;
						//System.out.println(LD[i-1][j-1]+" "+LD[i-1][j]+" "+LD[i][j-1]);
						//System.out.println(LD[i][j]);
					}
						
					
				}
			
			return LD[lengthA-1][lengthB-1];
		}

	
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值