有一个动态规划算法来寻找两个序列的最长公共子序列。如何找到两个序列X和Y的LCS算法(正确性测试)
(a) X = ABEDFEESTYH Y=ABEDFEESTYHABCDF
(b) X = BFAAAABBBBBJPRSTY Y=ABCDEFGHIJKLMNOPRS
(c) X = ϕ (Empty Sequence), Y = BABADCAB发布于 2011-11-24 21:25:44
这是一个在线计算器
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();
}
}发布于 2011-11-24 21:19:03
编辑:C++实现:http://comeoncodeon.wordpress.com/2009/08/07/longest-common-subsequence-lcs/
有一个动态编程算法可以找到两个序列的最长公共子序列(假设这就是您所说的LCS):http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
下面是(来自维基百科的)循环:

和伪代码(也来自维基百科):
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]然后就可以从C数组中重建最长的子序列(参见维基百科文章)。
发布于 2012-12-02 16:18:35
我已经用Ruby写了一个实现。它是String类的扩展:
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
复制相似问题