-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS.m
executable file
·86 lines (79 loc) · 1.86 KB
/
LCS.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function [D, dist, aLongestString] = LCS(C,x,y)
%%%Calculates the longest common substring between to strings.
%%%Code written by David Cumin
%%%email: d.cumin@auckland.ac.nz
%%%INPUT
%%%X, Y - both are strings e.g. 'test' or 'stingtocompare'
%%%OUTPUT
%%%D is the substring over the length of the shortest string
%%%dist is the length of the substring
%%%aLongestString is a sting of length dist (only one of potentially many)
%%%For example
%%% X = 'abcabc';
%%% Y = 'adcbac';
%%% [D dist str] = LCS(X,Y);
%%% results in:
%%% D = 0.6667
%%% dist = 4
%%% str = acbc
%%% this is seen for X: 'a-c-bc' and Y: 'a-cb-c'
%%%Make matrix
n =4;
m =4;
L=zeros(n+1,m+1);
L(1,:)=0;
L(:,1)=0;
b = zeros(n+1,m+1);
b(:,1)=1;%%%Up
b(1,:)=2;%%%Left
for i = 2:n+1
for j = 2:m+1
if strcmp(C{i-1}{x},C{j-1}{y})
L(i,j) = L(i-1,j-1) + 1;
b(i,j) = 3;%%%Up and left
else
L(i,j) = L(i-1,j-1);
end
if(L(i-1,j) >= L(i,j))
L(i,j) = L(i-1,j);
b(i,j) = 1;%Up
end
if(L(i,j-1) >= L(i,j))
L(i,j) = L(i,j-1);
b(i,j) = 2;%Left
end
end
end
L(:,1) = [];
L(1,:) = [];
b(:,1) = [];
b(1,:) = [];
dist = L(n,m);
D = dist;
if(dist == 0)
aLongestString = '';
else
%%%now backtrack to find the longest subsequence
i = n;
j = m;
p = dist;
aLongestString = {};
while(i>0 && j>0)
if(b(i,j) == 3)
aLongestString{p} = C{1}{i};
p = p-1;
i = i-1;
j = j-1;
elseif(b(i,j) == 1)
i = i-1;
elseif(b(i,j) == 2)
j = j-1;
end
end
if ischar(aLongestString{1})
aLongestString = char(aLongestString)';
else
aLongestString = cell2mat(aLongestString);
end
end
end