LCS ALGORITHM ( example )

…衆ロ難τιáo~ 提交于 2020-01-03 02:32:07

问题


There's a dynamic programming algorithm to find the Longest Common Subsequence of two sequences. How can I find the LCS algorithm of two sequences X and Y. (Test of correctness)

(a)   X  = ABEDFEESTYH  Y=ABEDFEESTYHABCDF

(b)  X =  BFAAAABBBBBJPRSTY  Y=ABCDEFGHIJKLMNOPRS

(c)  X = ϕ (Empty Sequence), Y = BABADCAB

回答1:


Here is an online calculator

http://igm.univ-mlv.fr/~lecroq/seqcomp/node4.html

Java

public class LCS {

    public static void main(String[] args) {
        String x = StdIn.readString();
        String y = StdIn.readString();
        int M = x.length();
        int N = y.length();

        // opt[i][j] = length of LCS of x[i..M] and y[j..N]
        int[][] opt = new int[M+1][N+1];

        // compute length of LCS and all subproblems via dynamic programming
        for (int i = M-1; i >= 0; i--) {
            for (int j = N-1; j >= 0; j--) {
                if (x.charAt(i) == y.charAt(j))
                    opt[i][j] = opt[i+1][j+1] + 1;
                else 
                    opt[i][j] = Math.max(opt[i+1][j], opt[i][j+1]);
            }
        }

        // recover LCS itself and print it to standard output
        int i = 0, j = 0;
        while(i < M && j < N) {
            if (x.charAt(i) == y.charAt(j)) {
                System.out.print(x.charAt(i));
                i++;
                j++;
            }
            else if (opt[i+1][j] >= opt[i][j+1]) i++;
            else                                 j++;
        }
        System.out.println();

    }

}



回答2:


EDIT: C++ implementation: http://comeoncodeon.wordpress.com/2009/08/07/longest-common-subsequence-lcs/

There's a dynamic programming algorithm to find the Longest Common Subsequence of two sequences (assuming that's what you meant by LCS): http://en.wikipedia.org/wiki/Longest_common_subsequence_problem

Here's the recurrence (from Wikipedia):

and the pseudocode (also from Wikipedia):

function LCSLength(X[1..m], Y[1..n])
    C = array(0..m, 0..n)
    for i := 0..m
       C[i,0] = 0
    for j := 0..n
       C[0,j] = 0
    for i := 1..m
        for j := 1..n
            if X[i] = Y[j]
                C[i,j] := C[i-1,j-1] + 1
            else:
                C[i,j] := max(C[i,j-1], C[i-1,j])
    return C[m,n]

It's then possible to reconstruct what the longest subsequences are from the C array (see the Wikipedia article).




回答3:


I have written a implementation in Ruby. It's a extension of String Class :

class String

def lcs_with(str)
    unless str.is_a? String
        raise ArgumentError,"Need a string"
    end

    n = self.length + 1
    m = str.length + 1

    matrix = Array.new(n, nil)
    matrix.each_index  do |i|
        matrix[i] = Array.new(m, 0)
    end

    1.upto(n - 1) do |i|
        1.upto(m - 1) do |j|
            if self[i] == str[j]
                matrix[i][j] = matrix[i - 1][j - 1] + 1
            else
                matrix[i][j] = [matrix[i][j - 1], matrix[i - 1][j]].max
            end
        end
    end
    matrix[self.length][str.length]
end 

end

puts "ABEDFEESTYH".lcs_with "ABEDFEESTYHABCDF"


来源:https://stackoverflow.com/questions/8257655/lcs-algorithm-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!