Code of bibin
<?
set_time_limit(60);
function cut($str,$cutLength)
{
return substr($str,$cutLength) . substr($str,0,$cutLength);
}
function shuffleStr($str,$cutLength)
{
for($i=0;$i<$cutLength;$i++)
{
$retStr .= substr($str,$cutLength+$i,1) . substr($str,$i,1);
}
return $retStr;
}
function flip($str)
{
return strrev($str);
}
function performAction($str,$cutLength,$startTime,$tmpCmd='',$tmpCnt=0)
{
$tempStr=$str;
while(!checkSequence($str,$cutLength))
{
$cnt++;
$randNum=rand(1,3);
switch($randNum)
{
case 1: $str=cut($str,$cutLength);
$cmd .= "C";
break;
case 2: $cmd .= "S";
$str=shuffleStr($str,$cutLength);
break;
default:
$cmd .= "F";
$str=flip($str);
break;
}
if($cnt>26)
{
$str=$tempStr;
$cnt=0;
$cmd='';
}
}
$et=gettimeofday();
$endTime=$et['sec'];
$diffTime=intval($endTime-$startTime);
if($diffTime<55)
{
if($cnt<=$tmpCnt || $tmpCnt==0)
{
$tmpCmd=$cmd;
$tmpCnt=$cnt;
}
if($tmpCnt<=$cutLength)
{
return $tmpCmd . $tmpCnt;
}
return performAction($tempStr,$cutLength,$startTime,$tmpCmd,$tmpCnt);
}
else
{
if(!empty($tmpCmd))
{
$cmd=$tmpCmd;
$cnt=$tmpCnt;
}
}
return $cmd . $cnt;
}
function checkSequence($str,$cutLength)
{
if(substr($str,0,$cutLength)!= substr(strtoupper($str),$cutLength))
return false;
for($i=0;$i<$cutLength;$i++)
{
$currentChar=substr($str,$i,1);
if(isset($tempVar) && ord($currentChar)< ord($tempVar))
{
return false;
}
else
{
$tempVar=substr($str,$i,1);
}
}
return true;
}
/****************************************************************
Hope this script wins the Zend Studio and 'PHP-Addict' t-shirt
*****************************************************************/
$tm=gettimeofday();
$startTime=$tm['sec'];
$filename='deck.txt';
$fp=fopen($filename,"r");
$str=trim(fread($fp,filesize($filename)));
fclose($fp);
$cutLength=strlen($str)/2;
$solution=performAction($str,$cutLength,$startTime);
echo $solution;
?>
Back to results
|
|