Code of dn
<?php function t_start ($name = 'default') { global $t_start_times; $t_start_times[$name] = explode(' ', microtime()); }
function t_current ($name = 'default') { global $t_start_times, $t_stop_times; if (!isset($t_start_times[$name])) { return 0; } if (!isset($t_stop_times[$name])) { $stop_time = explode(' ', microtime()); } else { $stop_time = $t_stop_times[$name]; } // do the big numbers first so the small ones aren’t lost $current = $stop_time[1] - $t_start_times[$name][1]; $current += $stop_time[0] - $t_start_times[$name][0]; return $current; }
function f($deckAr){ //flip... krsort($deckAr); foreach($deckAr as $val) $deckArNew[]=$val; return $deckArNew; }
function c($deckAr){ //cut... if($q)$chunks=array_chunk($deckAr,sizeof($deckAr)/2); else global $chunks; return array_merge($chunks[1],$chunks[0]); }
function r_s($deckAr){ //reverse shuffle... $chunks=array_chunk($deckAr,sizeof($deckAr)/2); $a=sizeof($deckAr)/2; for($i=0;$i<$a;$i++){ $deckArNew[]=$chunks[0][$i]; $deckArNew[]=$chunks[1][$i]; } return $deckArNew; }
function s($deckAr){ //shuffle... $chunks=array_chunk($deckAr,sizeof($deckAr)/2); $a=sizeof($deckAr)/2; for($i=0;$i<$a;$i++){ $deckArNew[]=$chunks[1][$i]; $deckArNew[]=$chunks[0][$i]; } return $deckArNew; }
function uns($deckAr){ //unshuffle... for($i=0;$i<sizeof($deckAr);$i=$i+2){ $chunks[1][]=$deckAr[$i]; $chunks[0][]=$deckAr[$i+1]; } return array_merge($chunks[0],$chunks[1]); }
function r_uns($deckAr){ //reverse unshuffle... for($i=0;$i<sizeof($deckAr);$i=$i+2){ $chunks[1][]=$deckAr[$i]; $chunks[0][]=$deckAr[$i+1]; } return array_merge($chunks[1],$chunks[0]); }
function check($deckArNew){ //final stage... global $ref,$sol; $chunks=array_chunk($deckArNew,sizeof($deckArNew)/2); if($chunks[1]==$ref['f']){$out=$sol."F"; die($out.strlen($out));} if($chunks[0]==$ref['c']){$out=$sol."C"; die($out.strlen($out));} if($chunks[0]==$ref['f']){$out=$sol."CF"; die($out.strlen($out));} if($chunks[1]==$ref['c']){$out=$sol; die($out.strlen($out));} }
function best($d){ global $best,$ref,$sol; $chunks=array_chunk($d,sizeof($d)/2); $d1=$chunks[1]; $a=sizeof($d1); for($i=0;$i<$a;$i++) if($d1[$i]==$ref['c'][$i])$x++; if($best[1]<$x)$best=array($sol,$x); }
function dc($deck){ //decompress... return explode(".",$deck); }
function searchallc($dAr){ //consequent search CS... $num=sizeof($dAr); #echo $num1=$num*$num; global $sol,$best,$tp; for($i=1;$i<43;$i++){ #echo t_current()."<br>"; $count=0; while($count<$i){ if($ar[$i-1]){ $tmp=$ar[$i-1]; unset($ar); while($val=each($tmp)){ $sol=$val[1]['sol']; $d=r_s(dc($val[1]['ar'])); $sol.="CS"; check($d); best($d); $ar[$i][]=array('ar'=>join(".",$d),'sol'=>$val[1]['sol']."CS"); } }else{ $d=$dAr; $d=r_s($dAr); $sol="CS"; check($d); best($d); $ar[$i][]=array('ar'=>join(".",$d),'sol'=>"CS"); } $count++; } } }
function searchall($dAr){ //consequent search S... $num=sizeof($dAr); #echo $num1=$num*$num; global $sol,$best,$tp; #echo "<br>"; for($i=1;$i<28;$i++){ #echo t_current()."<br>"; $count=0; while($count<$i){ if($ar[$i-1]){ $tmp=$ar[$i-1]; unset($ar); while($val=each($tmp)){ $sol=$val[1]['sol']; $d=s(dc($val[1]['ar'])); $sol.="S"; check($d); best($d); $ar[$i][]=array('ar'=>join(".",$d),'sol'=>$val[1]['sol']."S"); } }else{ $d=$dAr; $d=s($dAr); $sol="S"; check($d); best($d); $ar[$i][]=array('ar'=>join(".",$d),'sol'=>"S"); } $count++; } } }
function searchallf($dAr){ //consequent search Full... $num=sizeof($dAr); #echo $num1=$num*$num; global $sol,$ttt,$best,$tf; for($i=1;abs(t_current($ttt))+1<$tf;$i++){ $count=0; while($count<$i){ if($ar[$i-1]){ $tmp=$ar[$i-1]; unset($ar); while($val=each($tmp)){ if(abs(t_current($ttt))+1>$tf) return; $sol=$val[1]['sol']; $d=s(dc($val[1]['ar'])); $sol.="S"; check($d); best($d); $ar[$i][]=array('ar'=>join(".",$d),'sol'=>$val[1]['sol']."S"); $sol=$val[1]['sol']; $d=r_s(dc($val[1]['ar'])); $sol.="CS"; check($d); best($d); $ar[$i][]=array('ar'=>join(".",$d),'sol'=>$val[1]['sol']."CS"); } }else{ $d=$dAr; check($dAr); best($d); $d=s($dAr); $sol="S"; check($d); best($d); $ar[$i][]=array('ar'=>join(".",$d),'sol'=>"S"); $d=r_s($dAr); $sol="CS"; check($d); best($d); $ar[$i][]=array('ar'=>join(".",$d),'sol'=>"CS"); } $count++; } } }
$inpFile="deck.txt"; $tf=50; $best=array(0,0); t_start(); list($deck)=file($inpFile); //make arrays from given deck... $deckAr=explode(".",wordwrap(trim($deck),1,".",1)); $chunks=array_chunk($deckAr,sizeof($deckAr)/2); //make reference arrays... $sort=$deckAr; sort($sort); $chunks1=array_chunk($sort,sizeof($deckAr)/2); $refTmp=array_chunk(uns($sort),sizeof($deckAr)/2); $ref=array("f"=>$chunks1[0],"c"=>$chunks1[1],"s"=>$refTmp[0]); rsort($ref['f']); //launch function... $ttt="ok"; t_start($ttt); searchallf($deckAr);
searchall($deckAr); searchallc($deckAr); exit($best[0].strlen($best[0])); ?>
Back to results
|
|