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,操作的次数表示两个字符串的差异。
用于计算文本相关性、相似性
递归关系
两字符串长度为N、M,对1≤i≤N,1≤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=GGATCGA,B=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];
}
}