Longest common subsequence string LCS

11 visualizzazioni (ultimi 30 giorni)
Alaa
Alaa il 3 Giu 2011
[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 Commenti
Alaa
Alaa il 3 Giu 2011
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 il 3 Giu 2011
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

Accedi per commentare.

Risposte (1)

John D'Errico
John D'Errico il 3 Giu 2011
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.

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by