LCS (Longest Common Subsequence) JAVA 实现,与大家分享,希望在实现细节上,如可读性,所占空间与运行时间上面,能有更好建议...

import java.util.ArrayList;

/**
* @author marshal dong
*
*/
public class LongestCommonSubString
{

/**
* @param args
*/
public static void main(String[] args)
{
             String str1 = "abcdefg";
             String str2 = "bcdeabcdgfgadefg";
              LongestCommonSubString instance = new LongestCommonSubString(str1, str2);
              instance.printLongestCommonString();
}

private String str1, str2;

public LongestCommonSubString(String str1, String str2)
{
          this.str1 = str1;
            this.str2 = str2;
}

private int[][] matrix = null;

// the list to store their respective index of multiple common sequence
private ArrayList<Integer> longestStringIndexs = new ArrayList<Integer>();

// the length of the longest common sequence
private int longestStringLength = 0;

/**
* Using a matrix to store the common sequence between the two strings, if
* matrix[i][j]=1, it indicates the two strings have the same sequence
* matrix[i][j]; but we are not simply marking 1 for the same part, we also
* calculate the length of the sequence by statement : matrix[i][j] =
* matrix[i - 1][j - 1] + 1; This way, we eliminate the need of finding
* those value that consecutively equals 1 in their diagonal direction
*/
private void constructMatrix()
{
         if (str1 == null || str1.equals("") || str2 == null || str2.equals(""))
               return;

         int len1 = str1.length();
         int len2 = str2.length();
          matrix = new int[len1][len2];

for (int i = 0; i < len1; i++)
{
       for (int j = 0; j < len2; j++)
      {
           if (str1.charAt(i) == str2.charAt(j))
           {
                    if (j == 0 || i == 0)// in this case, we don't need to do
                                                 // addition
                               matrix[i][j] = 1;
                     else
                               matrix[i][j] = matrix[i - 1][j - 1] + 1;

                     if (matrix[i][j] > longestStringLength)
                    {
                            longestStringLength = matrix[i][j];
                            longestStringIndexs.clear();// clear the former index
                            longestStringIndexs.add(i); 
                     }
                     else if (matrix[i][j] == longestStringLength)
                    {
                            longestStringIndexs.add(i); // add the new index if
                                                                        // there are more than one
                                                                        // common stirng
                     }
                } 
            }
       }
}

public void printLongestCommonString()

             constructMatrix();
             printMatrix();

              for (Integer i : longestStringIndexs)
             {
                        System.out.println(str1.substring(i - longestStringLength + 1, i + 1)); 
             }
}

private void printMatrix()
  {
          if (matrix == null)
                 return;
          for (int i = 0; i < matrix.length; i++)
          {
                      for (int j = 0; j < matrix[i].length; j++)
                     {
                                 System.out.print(matrix[i][j] + " ");
                     }
                      System.out.println();
           }
  }

}

运行结果如下 :
0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 3 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 4 0 0 0 0 1 0 0 0
0 0 0 4 0 0 0 0 0 0 0 0 0 2 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 3 0
0 0 0 0 0 0 0 0 1 0 2 0 0 0 0 4
abcd
bcde
defg

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值