-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlcs.php
More file actions
46 lines (39 loc) · 666 Bytes
/
Copy pathlcs.php
File metadata and controls
46 lines (39 loc) · 666 Bytes
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
<?php
/**
* @file lcs.php
*
* Longest common subsequence
*
*/
//--------------------------------------------------------------------------------------------------
// from http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
function LCSLength($X, $Y)
{
$C = array();
$m = strlen($X);
$n = strlen($Y);
for ($i = 0; $i <= $m; $i++)
{
$C[$i][0] = 0;
}
for ($j = 0; $j <= $n; $j++)
{
$C[0][$j] = 0;
}
for ($i = 1; $i <= $m; $i++)
{
for ($j = 1; $j <= $n; $j++)
{
if ($X{$i-1} == $Y{$j-1})
{
$C[$i][$j] = $C[$i-1][$j-1]+1;
}
else
{
$C[$i][$j] = max($C[$i][$j-1], $C[$i-1][$j]);
}
}
}
return $C;
}
?>