Code of sergey
<?php
///////////////////////////////////////////
// Contest B1 (Flipping Shuflles)
// https://www.php-editors.com/contests.php
//
// (C)2003 Sergey Morgalev
//
// Email: sergo@bacup.ru
// PHP-Editors Forum nick: sergo
///////////////////////////////////////////
$timelimit = 60; // 60 seconds
$timelimit -= 4;
function getmicrotime()
{
return time();
//list ($usec, $sec) = explode (" ",microtime());
//return ((float)$usec + (float)$sec);
}
function s_cut (&$str)
{
return substr ($str, $GLOBALS['HALFLEN'],
$GLOBALS['HALFLEN']).substr ($str, 0, $GLOBALS['HALFLEN']);
}
function s_flip (&$str)
{
return strrev ($str);
}
// forward shuffle (fastest)
function s_shuffle (&$str)
{
$len = $pos = $GLOBALS['HALFLEN'];
$spos = 0;
$str1 = '';
while ($len--)
{
$str1 .= $str[$pos++].$str[$spos++];
}
return $str1;
}
function init_str ($str)
{
$len = strlen ($str);
$GLOBALS['STRLEN'] = $len;
$GLOBALS['HALFLEN'] = $len /= 2;
$res_str = '';
$c = 65;
while ($len--)
{
$res_str .= chr ($c++);
}
$res_str .= strtolower ($res_str);
$GLOBALS['RESULT'] = $res_str;
$GLOBALS['results'] = array();
//$GLOBALS['results'][$str] = '';
$GLOBALS['nodeset'] = array();
$GLOBALS['nodeset'][$str] = '';
$GLOBALS['score'] = 0;
$GLOBALS['best_res'] = $str;
$GLOBALS['found'] = false;
}
function s_score (&$str)
{
return substr_count ($GLOBALS['RESULT'] ^ $str, chr(0));
}
function s_apply ($str, $state, $new_state, $func, &$nodeset, $add = false)
{
$newstr = $func ($str);
if ($newstr === $GLOBALS['RESULT']) // done
{
$GLOBALS['best_res'] = $state.$new_state;
exit();
}
if (!isset ($GLOBALS['results'][$newstr]))
{
// register in $GLOBALS['results'], add to nodeset
$GLOBALS['results'][$newstr] = $state = $state.$new_state;
if ($add)
{
$nodeset[$newstr] = $state;
}
// count score
if (($score = s_score ($newstr)) > $GLOBALS['score'])
{
$GLOBALS['score'] = $score;
$GLOBALS['best_res'] = $state;
}
}
return $newstr;
}
// horizontal
function build_nodeset()
{
$nodeset = array();
declare (ticks=32) {
foreach ($GLOBALS['nodeset'] as $str => $state)
{
// S
s_apply ($str, $state, 'S', 's_shuffle', $nodeset, true);
// CS
s_apply (s_apply (
$str, $state, 'C', 's_cut', $nodeset),
$state.'C', 'S', 's_shuffle', $nodeset, true);
// FCS
s_apply (s_apply (s_apply (
$str, $state, 'F', 's_flip', $nodeset),
$state.'F', 'C', 's_cut', $nodeset),
$state.'FC', 'S', 's_shuffle', $nodeset, true);
}}
$GLOBALS['nodeset'] = &$nodeset;
}
function bye()
{
//echo sizeof ($GLOBALS['results'])."\n";
//echo (getmicrotime() - $GLOBALS['start'])."\n";
echo $GLOBALS['best_res'].strlen ($GLOBALS['best_res']);
}
function time_count()
{
static $time = 0;
$time = getmicrotime() - $GLOBALS['start'];
if ($GLOBALS['timelimit'] < $time)
{
exit();
}
}
register_shutdown_function ('bye');
register_tick_function ('time_count');
$start = getmicrotime();
$deck = trim (fread ($fd = fopen ('deck.txt', 'r'), filesize ('deck.txt')));
fclose ($fd);
init_str ($deck);
while (1)
{
build_nodeset();
}
?>
Back to results
|
|