|
|||||||||||||
".$array." ".count($array)." "; $size_best=count($array); $array_final = array_reverse($array); echo "
".$x." ".$y." ".$array; $best_move=min($moves); if ($best_move == $matrix[$x-1][$y-1]){ $array[] = $matrix[$x-1][$y-1]; $new_template[]=$template[$x-1]; $new_data[]=$data[$y-1]; find_best($x-1, $y-1, $array, $matrix, $data, $template, $new_data, $new_template);} elseif ($best_move == $matrix[$x][$y-1]){ $array[]=$matrix[$x][$y-1]; $new_template[]='-'; $new_data[]=$data[$y-1]; find_best($x, $y-1, $array, $matrix, $data, $template, $new_data, $new_template);} elseif ($best_move == $matrix[$x-1][$y]){ $array[]=$matrix[$x-1][$y]; $new_template[]=$template[$x-1]; $new_data[]='-'; find_best($x-1, $y, $array, $matrix, $data, $template, $new_data, $new_template);} else{ echo "Error";} } } $data=array('chk', 'wt', 'chng', 'chk', 'tt', 'trha', 'htrha', 'rnst', 'trha', 'sdsur', 'sdsur', 'sdsun', 'sdsur', 'sdsun', 'sdsur', 'rwt', 'rwt', 'exam', 'exam', 'exam', 'hall', 'trha', 'htrha', 'trha', 'tt', 'tr', 'htrha', 'sdsur', 'sdsun'); $template=array('chk', 'wt', 'chng', 'sdsur', 'hall', 'exam', 'hall', 'trha', 'tr', 'sdsur'); $size_data=count($data); $size_template=count($template); $g=1; $d=1; $matrix[0][0]=0; for($x = 1; $x < ($size_data + 1); $x++){ $matrix[0][$x]=$matrix[0][$x-1] + $g; echo "matrix[0][".$x."] = ".$matrix[0][$x]." "; } for($y = 1; $y < ($size_template + 1); $y++){ $matrix[$y][0]=$matrix[$y-1][0] + $g; echo "matrix[".$y."][0] = ".$matrix[$y][0]." "; } for($w = 1; $w < ($size_template + 1); $w++){ for ($v = 1; $v < ($size_data + 1); $v++){ if($template[$w-1] == $data[$v-1]){ echo "template[".($w-1)."] = ".$template[$w-1]." and data[".($v-1)."] = ".$data[$v-1]." "; echo "Compare ".$matrix[$w-1][$v-1]." ".($matrix[$w-1][$v] + $g)." ".($matrix[$w][$v-1] + $g)." "; $matrix[$w][$v]=min($matrix[$w-1][$v-1], ($matrix[$w-1][$v] + $g), ($matrix[$w][$v-1] + $g)); } else{ echo "template[".($w-1)."] = ".$template[$w-1]." and data[".($v-1)."] = ".$data[$v-1]." "; echo "Compare ".($matrix[$w-1][$v-1] + $d)." ".($matrix[$w-1][$v] + $g)." ".($matrix[$w][$v-1] + $g)." "; $matrix[$w][$v]=min(($matrix[$w-1][$v-1] + $d), ($matrix[$w-1][$v] + $g), ($matrix[$w][$v-1] + $g)); } } } display_all($matrix, $data, $template); $initial=array($matrix[$size_template-1][$size_data-1]); $best_sequence = find_best($size_template, $size_data, $initial, $matrix, $data, $template, array(), array()); $size_best=count($best_sequence); echo $best_sequence." "; echo $size_best." "; for ($s = 0; $s < $size_best; $s++){ echo $s." "; } ?> |