22namespace webd \language ;
33
44/**
5- * The longest common subsequence (LCS) problem consists in finding the
6- * longest subsequence common to two (or more) sequences. It differs from
7- * problems of finding common substrings: unlike substrings, subsequences are
8- * not required to occupy consecutive positions within the original sequences.
9- *
5+ * The longest common subsequence (LCS) problem consists in finding the
6+ * longest subsequence common to two (or more) sequences. It differs from
7+ * problems of finding common substrings: unlike substrings, subsequences are
8+ * not required to occupy consecutive positions within the original sequences.
9+ *
1010 * Used by the diff utility, by Git for reconciling multiple changes, etc.
1111 */
1212class LCS
@@ -15,7 +15,8 @@ class LCS
1515 private $ X = "" ;
1616 private $ Y = "" ;
1717
18- public function __construct ($ str1 , $ str2 ) {
18+ public function __construct ($ str1 , $ str2 )
19+ {
1920
2021 $ this ->X = $ str1 ;
2122 $ this ->Y = $ str2 ;
@@ -26,7 +27,7 @@ public function __construct($str1, $str2) {
2627 $ this ->C = array ();
2728
2829 for ($ i = 0 ; $ i <= $ m ; $ i ++) {
29- $ this ->C [$ i ][0 ] = 0 ;
30+ $ this ->C [$ i ][0 ] = 0 ;
3031 }
3132
3233 for ($ j = 0 ; $ j <= $ n ; $ j ++) {
@@ -37,7 +38,6 @@ public function __construct($str1, $str2) {
3738 for ($ j = 1 ; $ j <= $ n ; $ j ++) {
3839 if ($ str1 [$ i -1 ] == $ str2 [$ j -1 ]) {
3940 $ this ->C [$ i ][$ j ] = $ this ->C [$ i -1 ][$ j -1 ] + 1 ;
40-
4141 } else {
4242 $ this ->C [$ i ][$ j ] = max ($ this ->C [$ i ][$ j -1 ], $ this ->C [$ i -1 ][$ j ]);
4343 }
@@ -52,31 +52,36 @@ public function __construct($str1, $str2) {
5252 // C[i,j] := max(C[i,j-1], C[i-1,j])
5353 }
5454
55- public function length () {
55+ public function length ()
56+ {
5657 return $ this ->C [strlen ($ this ->X )][strlen ($ this ->Y )];
5758 }
5859
59- public function __toString () {
60+ public function __toString ()
61+ {
6062 return $ this ->value ();
6163 }
6264
63- public function value () {
65+ public function value ()
66+ {
6467 return $ this ->backtrack (strlen ($ this ->X ), strlen ($ this ->Y ));
6568 }
6669
6770 /**
68- * Edit distance when only insertion and deletion is allowed (no
71+ * Edit distance when only insertion and deletion is allowed (no
6972 * substitution)
7073 * = strlen(str1) + strlen(str2) - 2 * length(LCS(str1, str2))
7174 * @param type $string1
7275 * @param type $string2
7376 */
74- public function distance () {
77+ public function distance ()
78+ {
7579 return strlen ($ this ->X ) + strlen ($ this ->Y ) - 2 * $ this ->length ();
7680 }
7781
7882
79- private function backtrack ($ i , $ j ) {
83+ private function backtrack ($ i , $ j )
84+ {
8085 if ($ i == 0 || $ j == 0 ) {
8186 return "" ;
8287 }
@@ -104,7 +109,7 @@ private function backtrack($i, $j) {
104109 }
105110
106111// /**
107- // * Edit distance when only insertion and deletion is allowed (no
112+ // * Edit distance when only insertion and deletion is allowed (no
108113// * substitution)
109114// * = strlen(str1) + strlen(str2) - 2 * length(LCS(str1, str2))
110115// * @param type $string1
@@ -113,19 +118,19 @@ private function backtrack($i, $j) {
113118// public static function distance($str1, $str2) {
114119// return strlen($str1) + strlen($str2) - 2 * self::length($str1, $str2);
115120// }
116- //
121+ //
117122// public static function lcs($str1, $str2) {
118123// $lcs = new LCS($str1, $str2);
119124// return $lcs->backtrack(strlen($str1), strlen($str2));
120125// }
121126// /**
122- // *
127+ // *
123128// * @param type $string1
124129// * @param type $string2
125130// */
126131// public static function length($string1, $string2) {
127132// $lcs = new LCS($str1, $str2);
128133// return $lcs->C;
129- //
134+ //
130135// }
131- }
136+ }
0 commit comments