-
Notifications
You must be signed in to change notification settings - Fork 2
/
lcs.php
223 lines (189 loc) · 4.42 KB
/
lcs.php
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
// $Id: $
/**
* @file lcs.php
*
* String comparison functions, such as longest common subsequence and longest common string
*
*/
/**
* @brief Find longest common subsequence of two strings
*
* Code based on http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
*
* Implemented as a class because we may want to retain results for display (e.g., debugging)
*
*/
class LongestCommonSequence
{
var $left;
var $right;
var $C = array();
var $X;
var $Y;
//----------------------------------------------------------------------------------------------
function LongestCommonSequence ($X, $Y)
{
$this->left = '';
$this->right = '';
$this->X = $X;
$this->Y = $Y;
}
//----------------------------------------------------------------------------------------------
// from http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
function compare()
{
$m = strlen($this->X);
$n = strlen($this->Y);
for ($i = 0; $i <= $m; $i++)
{
$this->C[$i][0] = 0;
}
for ($j = 0; $j <= $n; $j++)
{
$this->C[0][$j] = 0;
}
for ($i = 1; $i <= $m; $i++)
{
for ($j = 1; $j <= $n; $j++)
{
if ($this->X{$i-1} == $this->Y{$j-1})
{
$this->C[$i][$j] = $this->C[$i-1][$j-1]+1;
}
else
{
$this->C[$i][$j] = max($this->C[$i][$j-1], $this->C[$i-1][$j]);
}
}
}
}
//----------------------------------------------------------------------------------------------
function score()
{
$this->compare();
$this->printDiff($this->C, $this->X, $this->Y, strlen($this->X), strlen($this->Y));
return $this->C[strlen($this->X)][strlen($this->Y)];
}
//----------------------------------------------------------------------------------------------
function diff()
{
$length_cs = $this->score();
$d = (strlen($this->X) + strlen($this->Y) - (2 * $length_cs))/(strlen($this->X) + strlen($this->Y));
return $d;
}
//----------------------------------------------------------------------------------------------
function display()
{
$html = '<div>' . $this->left . '<br/>' . $this->right . '</div>';
return $html;
}
//----------------------------------------------------------------------------------------------
function printDiff($C, $X, $Y, $i, $j)
{
if (($i > 0) and ($j > 0) and ($X{$i-1} == $Y{$j-1}))
{
$this->printDiff($C, $X, $Y, $i-1, $j-1);
//echo " " , $X{$i-1};
$this->left .= "<span style=\"background:rgb(100,255,100);color:black;\">" . $X{$i-1} . "</span>";
$this->right .= "<span style=\"background:rgb(100,255,100);color:black;\">" . $X{$i-1} . "</span>";
}
else
{
if (($j > 0) and ($i == 0 or $C[$i][$j-1] >= $C[$i-1][$j]))
{
$this->printDiff($C, $X, $Y, $i, $j-1);
//echo "+ " , $Y{$j-1};
$this->right .= $Y{$j-1};
}
else
{
if (($i > 0) and ($j == 0 or $C[$i][$j-1] < $C[$i-1][$j]))
{
$this->printDiff($C, $X, $Y, $i-1, $j);
//echo "- " , $X{$i-1};
$this->left .= $X{$i-1};
}
}
}
}
}
//--------------------------------------------------------------------------------------------------
/**
* @brief Find longest common substring of two strings
*
* Code based on http://en.wikipedia.org/wiki/Longest_common_substring_problem
*
* @param S string one
* @param T string two
* @param str On return holds the longest common substring
*
* @return Length of longest common substring
*/
function LongestCommonSubstring($S, $T, &$str)
{
$str = '';
$ret = array();
$m = strlen($S);
$n = strlen($T);
$L = array();
for ($i = 0; $i < $m; $i++)
{
$L[$i] = array();
for ($j = 0; $j < $n; $j++)
{
$L[$i][$j] = 0;
}
}
$z = 0;
for ($i = 0; $i < $m; $i++)
{
for ($j = 0; $j < $n; $j++)
{
if ($S{$i} == $T{$j})
{
if ($i == 0 || $j == 0)
{
$L[$i][$j] = 1;
}
else
{
$L[$i][$j] = $L[$i-1][$j-1] + 1;
}
if ($L[$i][$j] > $z)
{
$z = $L[$i][$j];
$ret = array();
}
if ($L[$i][$j] == $z)
{
$str = substr($S, $i - $z + 1, $z);
array_push($ret, $str);
}
}
}
}
// print_r($ret);
if (isset($ret[0]))
{
$str = $ret[0];
}
// echo "z=$z\n";
return $z;
}
/*
// test
$s1 = 'hello blue marine';
$s2 = 'yellow blue submarine';
$s1 = 'Hollenb.';
$s2 = 'G. J. Hollenberg';
$s1 = 'x, 1880';
$s2 = 'Milne Edwards, 1880';
$lcs = new LongestCommonSequence($s1,$s2);
//echo $lcs->score();
//echo $lcs->display();
$str = '';
echo LongestCommonSubstring($s1,$s2, $str);
echo "str=$str\n";
*/
?>