zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

第3章 结构之法——计算字符串的相似度

计算 字符串 结构 相似
2023-09-11 14:20:30 时间

计算字符串的相似度

问题描述

分析与解法

具体代码如下:

 1 package chapter3jiegouzhifa.StringSimilarity;
 2 /**
 3  * 计算字符串的相似度
 4  * 【解法一】
 5  * @author DELL
 6  *
 7  */
 8 public class StringSimilarity {
 9     public static int CalculateStringDistance(String str1, int b1, int e1, String str2, int b2, int e2){
10         if(b1>e1){
11             if(b2>e2)
12                 return 0;
13             else
14                 return e2-b2+1;
15         }
16         if(b2>e2){
17             if(b1>e1)
18                 return 0;
19             else
20                 return e1-b1+1;
21         }
22         if(str1.charAt(b1)==str2.charAt(b2)){
23             return CalculateStringDistance(str1,b1+1,e1,str2,b2+1,e2);
24         }else{
25             int t1 = CalculateStringDistance(str1,b1+1,e1,str2,b2,e2);
26             int t2 = CalculateStringDistance(str1,b1,e1,str2,b2+1,e2);
27             int t3 = CalculateStringDistance(str1,b1+1,e1,str2,b2+1,e2);
28             return Math.min(Math.min(t1, t2), t3)+1;
29         }
30     }
31     public static void main(String[] args) {
32         String s1 = "xabcdae";
33         String s2 = "xfdfa";
34         System.out.println(s1+"与"+s2+"的距离为:"+CalculateStringDistance(s1,0,s1.length()-1,s2,0,s2.length()-1));
35 
36     }
37 
38 }

程序运行结果如下:

xabcdae与xfdfa的距离为:5

 下述代码清单3-6有误,最后的t1,t2,t3计算错误。

改进后的程序如下:

 

 1 package chapter3jiegouzhifa.StringSimilarity;
 2 /**
 3  * 计算字符串的相似度
 4  * 【解法二】改进算法
 5  * 用空间换时间
 6  * @author DELL
 7  *
 8  */
 9 public class StringSimilarity2 {
10     public static int CalculateStringDistance(String str1, int b1, int e1, String str2, int b2, int e2){
11         int s[][][][] = new int[str1.length()+1][str1.length()][str2.length()+1][str2.length()];
12         if(b1>e1){
13             if(b2>e2)
14                 return 0;
15             else
16                 return e2-b2+1;
17         }
18         if(b2>e2){
19             if(b1>e1)
20                 return 0;
21             else
22                 return e1-b1+1;
23         }
24         if(s[b1][e1][b2][e2]!=0)
25             return s[b1][e1][b2][e2];
26         else{
27             if(str1.charAt(b1)==str2.charAt(b2)){            
28                 s[b1][e1][b2][e2]=CalculateStringDistance(str1,b1+1,e1,str2,b2+1,e2);
29                 return s[b1][e1][b2][e2];
30             }else{
31                 int t1,t2,t3;
32                 if(s[b1+1][e1][b2][e2]!=0)
33                     t1 = s[b1+1][e1][b2][e2];
34                 else{
35                     s[b1+1][e1][b2][e2] = CalculateStringDistance(str1,b1+1,e1,str2,b2,e2);
36                     t1 = s[b1+1][e1][b2][e2];
37                 }
38                 if(s[b1][e1][b2+1][e2]!=0)
39                     t2 = s[b1][e1][b2+1][e2];
40                 else{
41                     s[b1][e1][b2+1][e2] = CalculateStringDistance(str1,b1,e1,str2,b2+1,e2);
42                     t2 = s[b1][e1][b2+1][e2];
43                 }
44                 if(s[b1+1][e1][b2+1][e2]!=0)
45                     t3 = s[b1+1][e1][b2+1][e2];
46                 else{
47                     s[b1+1][e1][b2+1][e2] = CalculateStringDistance(str1,b1+1,e1,str2,b2+1,e2);
48                     t3 = s[b1+1][e1][b2+1][e2];
49                 }
50                 s[b1][e1][b2][e2] = Math.min(Math.min(t1, t2), t3)+1;
51                 return s[b1][e1][b2][e2];
52             }
53         }    
54     }
55     public static void main(String[] args) {
56         String s1 = "xabcdae";
57         String s2 = "xfdfa";
58         System.out.println(s1+"与"+s2+"的距离为:"+CalculateStringDistance(s1,0,s1.length()-1,s2,0,s2.length()-1));
59 
60     }
61 
62 }

程序运行结果如下:

xabcdae与xfdfa的距离为:5