最长公共子串- LCS 算法

最长公共子串- LCS 算法

 LCS (Longest Common Subsequence) 算法

已知字符串str1="网站高并发解决方案",str2="如何解决网站高并发",如何字符串最长公共子串?

lcs 算法原理

将2个字符串采用行列 排列:

 

                 

                 

                 

                 

                 

                 

                 

                 

             

如果行列里面的字符相同,则表示1,否则为0:

 

0

0

0

0

1

0

0

0

0

0

0

0

0

0

1

0

0

0

0

0

0

0

0

0

1

0

0

0

0

0

0

0

0

0

1

0

0

0

0

0

0

0

0

0

1

0

0

1

0

0

0

0

0

0

0

0

0

1

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

同时我们可以优化: 很明显,通过坐标可看到,相同的坐标已经标位1,通过计算连续对角线长度,即可比对出最长字符串.

如果行列里面的字符不相同,则表示为0,否则表示为 该坐标左上角的值后再加1:

 

0

0

0

0

1

0

0

0

0

0

0

0

0

0

2

0

0

0

0

0

0

0

0

0

3

0

0

0

0

0

0

0

0

0

4

0

0

0

0

0

0

0

0

0

5

0

0

1

0

0

0

0

0

0

0

0

0

2

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

在判断字符串时,记录当前最大值与当前最大值坐标,判断完毕之后,即可通过记录的最大坐标获取到最长字符串最后的坐标值

 

 public String LCS (String str1, String str2) {
   // write code here
        int m = str1.length();
        int n = str2.length();
        int[][] dp = new int[m][n];
        int maxLength = 0;
        int lastIndex = 0;     //用来记录str1中最长公共串的最后一个字符的下标
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(str1.charAt(i) == str2.charAt(j)){   //判断str1中第i个字符是否和str2中第j个字符相等
                    if(i == 0 || j == 0){              
                        dp[i][j] = 1;
                    }else{
                        dp[i][j] = dp[i - 1][j - 1] + 1;    
                    }
                     
                    if(dp[i][j] > maxLength){    //判断是否需要更新最长公共子串
                        maxLength = dp[i][j];
                        lastIndex = i;
                    }
                }
            }
        }
         
        //通过str1来截取长度为maxLength, 最后字符坐标为lastIndex的子串
        return str1.substring(lastIndex - maxLength + 1, lastIndex + 1);    
    }

  

posted @ 2022-04-29 11:48  abcdefghijklmnop  阅读(184)  评论(0编辑  收藏  举报