Longest common subsequence string LCS

11 次查看(过去 30 天)
Alaa
Alaa 2011-6-3
[EDIT: 20110603 09:53 CDT - reformat - WDR]
can some one help correct this matlab code for the recursive Longest common subsequence string LCS, the error "Subscripted assignment dimension" when run the following code
the function rec read two string from files the call the temp to fill the LCS table recursivly
function rec()
%to read the two strings from files
%x1='xyxxzxyzxy';
%x2='zxzyyzxxyxxz';
fid1=fopen('file1.txt');
fid2=fopen('file2.txt');
chs1 = textscan(fid1, '%c');
chs2 = textscan(fid2, '%c');
fclose(fid1);
fclose(fid2);
global x1;
global x2;
x1 =char(chs1);
x2 =char(chs2);
%to read the two strings from files
%creat a global table then call temp func to fill in
lenx1= length(x1)+1;
lenx2= length(x2)+1;
global L;
L = zeros(lenx1,lenx2);
temp (lenx1,lenx2);
%disp (L);
end
function result = temp (m,n)
global x1;
global x2;
global L;
result = L;
if m==1 || n==1
result(m,n) = 0;
elseif x1(m-1) == x2(n-1)
disp(['if=' x1(m-1)]);
disp(['if=' x2(n-1)]);
result(m,n) = temp(m-1,n-1) + 1;
else
disp(['else=' x1(m-1)]);
disp(['else=' x2(n-1)]);
result= max(temp(m,n-1), temp(m-1,n));
end
%L(m,n) = result(m,n);
return
end
  3 个评论
Alaa
Alaa 2011-6-3
you can add any two strings...the error occure when the function temp return from the base state "if m==1||n==1" to start filling the table L
Alaa
Alaa 2011-6-3
I will be thanful if you try to help me ....
this link descibe the problem i need to code it
http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#LCS_function_defined

请先登录,再进行评论。

回答(1 个)

John D'Errico
John D'Errico 2011-6-3
Well, you could always just use my commonsubstring function to do the work.
Of course, since this is almost surely homework, that is not really a viable option. commonsubstring is pretty fast though.

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by