Example #1
0
$data = array(100, 4, 200, 1, 3, 2);
function lcs($data)
{
    if (count($data) == 0) {
        return 0;
    }
    // Use a hashSet
    foreach ($data as $key => $value) {
        $result[$value] = 1;
    }
    $max = 1;
    for ($i = 0; $i < count($data); $i++) {
        $left = $data[$i] - 1;
        $right = $data[$i] + 1;
        $count = 1;
        while (in_array($left, array_keys($result))) {
            $count++;
            unset($result[$left]);
            $left--;
        }
        while (in_array($right, array_keys($result))) {
            $count++;
            unset($result[$right]);
            $right++;
        }
        $max = max($count, $max);
    }
    return $max;
}
var_dump(lcs($data));
//4
Example #2
0
 public function comparer($new, $old)
 {
     $paras = $this->diff->segmenter($new);
     $paras_old = $this->diff->segmenter($old);
     if ($this->diff->fuzzy()) {
         list($trans_rev, $trans) = apparier_paras($paras_old, $paras);
         $lcs = lcs_opt($trans);
         $lcs_rev = array_flip($lcs);
     } else {
         list($trans_rev, $trans) = lcs($paras_old, $paras);
         $lcs = $trans;
         $lcs_rev = $trans_rev;
     }
     reset($paras_old);
     reset($paras);
     reset($lcs);
     unset($i_old);
     $fin_old = false;
     foreach ($paras as $i => $p) {
         if (!isset($trans[$i])) {
             // Paragraphe ajoute
             $this->diff->ajouter($p);
             continue;
         }
         $j = $trans[$i];
         if (!isset($lcs[$i])) {
             // Paragraphe deplace
             $this->diff->deplacer($p, $paras_old[$j]);
             continue;
         }
         if (!$fin_old) {
             // Paragraphes supprimes jusqu'au paragraphe courant
             if (!isset($i_old)) {
                 list($i_old, $p_old) = each($paras_old);
                 if (!$p_old) {
                     $fin_old = true;
                 }
             }
             while (!$fin_old && $i_old < $j) {
                 if (!isset($trans_rev[$i_old])) {
                     $this->diff->supprimer($p_old);
                 }
                 unset($i_old);
                 list($i_old, $p_old) = each($paras_old);
                 if (!$p_old) {
                     $fin_old = true;
                 }
             }
         }
         // Paragraphe n'ayant pas change de place
         $this->diff->comparer($p, $paras_old[$j]);
     }
     // Paragraphes supprimes a la fin du texte
     if (!$fin_old) {
         if (!isset($i_old)) {
             list($i_old, $p_old) = each($paras_old);
             if (!strlen($p_old)) {
                 $fin_old = true;
             }
         }
         while (!$fin_old) {
             if (!isset($trans_rev[$i_old])) {
                 $this->diff->supprimer($p_old);
             }
             list($i_old, $p_old) = each($paras_old);
             if (!$p_old) {
                 $fin_old = true;
             }
         }
     }
     if (isset($i_old)) {
         if (!isset($trans_rev[$i_old])) {
             $this->diff->supprimer($p_old);
         }
     }
     return $this->diff->resultat();
 }
Example #3
0
    $data[] = fgets($inputFile);
}
fclose($inputFile);
$t = explode(' ', trim($data[0]));
$s1 = explode(' ', trim($data[1]));
$s2 = explode(' ', trim($data[2]));
$n = $t[0];
$m = $t[1];
//AATCC
//$s1 = array('A', 'A', 'T', 'C', 'C');
//$s1 = 'AATCC';
//ACACG
//$s2 = array('A', 'C', 'A', 'C', 'G');
//$s2 = 'ACACG';
$result = array();
$c = lcs($s1, $s2);
backtrack($c, $s1, $s2, count($s1), count($s2), $result);
echo implode(' ', array_reverse($result));
function lcs($s1, $s2)
{
    $length1 = count($s1);
    //strlen($s1);
    $length2 = count($s2);
    //strlen($s2);
    $c = array();
    for ($i = 0; $i < $length1 + 1; $i++) {
        for ($j = 0; $j < $length2 + 1; $j++) {
            $c[$i][$j] = 0;
        }
    }
    for ($i = 1; $i < $length1 + 1; $i++) {