Code of barry
<html>
<!-- Creation date: 24/05/03 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Contest 3b [Barry A Andrew]</title>
<meta name="Description" content="">
<meta name="Keywords" content="">
<meta name="Author" content="Barry A Andrew">
<meta name="Generator" content="AceHTML 4 Pro">
</head>
<body>
<?php
/********************************************************************
Contest 3b - by Barry A Andrew <barryaandrew@aol.com>
********************************************************************/
function lexroot ($str) {
// sort string into alphabetical order
$a = array();
for ($i=0; $i<strlen($str); $i++) $a[] = $str[$i]; // put chars in array
sort($a);
return join('',$a);
}
/********************************************************************/
function readLists(&$L, &$S) {
$dir = dirname($_SERVER[PATH_TRANSLATED]);
$f = file("$dir/lists.txt");
$i = 0;
$dolist = 1;
foreach ($f as $buffer) {
if ($dolist) {
if (strpos($buffer,",") > 0) {
$dolist = 0;
$buffer = str_replace(","," ",$buffer);
}
$L[$i] .= trim($buffer) . ' ';
}
else {
if (strpos($buffer,".") > 0) {
$dolist = 1;
$buffer = str_replace("."," ",$buffer);
}
$S[$i] .= trim($buffer);
if ($dolist) $i++;
}
}
}
/********************************************************************/
function noblank($item) { // filter out blanks function
return ($item != '');
}
function bylen($a,$b) { // sort descending length function
$la = $a['len'];
$lb = $b['len'];
if ($la==$lb) return 0;
else return ($la > $lb) ? -1 : 1;
}
function prepareLists(&$L,&$S) {
for ($i=0; $i < count($S); $i++) {
$S[$i] = trim(lexroot($S[$i])); // put sentence in alpha sequence
$a = explode(' ',$L[$i]); // put word list in temp array
$a = array_filter($a,'noblank'); // clear out any accidental blanks
$L[$i] = $a; // store word list as array
}
}
/********************************************************************/
function FindWord($word, &$avail) {
$missing = '';
$len = strlen($word);
$xword = lexroot($word);
$p=0;
// search available letters for those in the word
// eliminating those used and storing missing letters
for ($i=0; $i<$len; $i++) {
if (($p=strpos($avail,$xword[$i],$p))!==false) {
$avail[$p]='_';
}
else {
$missing .= $xword[$i];
}
}
return $missing;
}
/********************************************************************/
function red($L) {
return "<font color=\"#ff0000\">$L</font>";
}
/********************************************************************/
function Process($proctype, $L, $S, &$resultWords, &$resultCount) {
$words = $L;
$avail = $S;
$blanks = 2;
$usedblank = '';
$resultW = array();
$resultC = 0;
foreach ($words as $wd=>$attr) {
$save = $avail; // store available letters so they can be restored later if need be
$restore = 0;
$word = $attr['word'];
$res = FindWord($word,$avail); // get missing letters, if any
// drop in the blanks if needed
switch (strlen($res)) {
case 0:
$attr['output'] = $word;
$resultW[] = $attr;
$resultC += strlen($word);
break;
case 1:
if (($blanks > 0) && ($usedblank != $res)) {
$usedblank = $res;
$blanks--;
$p = strpos($word,$res);
$attr['output'] = substr($word,0,$p) . red($res) . substr($word,$p+1).' ';
$resultW[] = $attr;
$resultC += strlen($word);
} else $restore = 1;
break;
case 2:
if (($proctype==1) || ($blanks < 2) || ($res[0]==$res[1])) {
$restore = 1;
break;
}
$blanks=0;
$p = min(strpos($word,$res[0]),strpos($word,$res[1]));
$q = max(strpos($word,$res[0]),strpos($word,$res[1]));
$a = $word[$p];
$b = $word[$q];
$attr['output'] = substr($word,0,$p) . red($a) . substr($word,$p+1,$q-$p-1) .
red($b) . substr($word,$q+1) . ' ';
$resultW[] = $attr;
$resultC += strlen($word);
break;
default : // 3 or more blanks required
$restore = 1;
} //switch
if ($restore) $avail = $save;
} // foreach words
$resultWords[] = $resultW;
$resultCount[] = $resultC;
}
/********************************************************************/
function Preprocess($L, $S, &$short) {
$words = $L;
$avail = $S;
foreach ($words as $word) {
$avail = $S;
$res = FindWord($word,$avail);
if (strlen($res) < 3) {
$tmp=array();
$tmp['word'] = $word;
$tmp['len'] = strlen($word);
$tmp['output'] = '';
$short[] = $tmp;
}
}
}
/********************************************************************/
function combos($a,$b) {
if (count($a)==1)
return $b.$a[0].'|';
else {
for ($i=0; $i<count($a); $i++) {
$t = $a;
$x = $t[$i];
unset($t[$i]);
$t = array_values($t);
$result .= combos($t, $b.$x.',');
}
return $result;
}
}
/********************************************************************/
function Resequence($a, $s) {
$res = array();
$x = explode(',',$s);
foreach($x as $seq) $res[] = $a[$seq];
return $res;
}
/********************************************************************/
$list = array();
$sent = array();
$finalWords = '';
$finalCount = 0;
readLists($list, $sent);
prepareLists($list, $sent);
for ($i=0; $i<count($list); $i++) {
$resultWords = array();
$resultCount = array();
$shortlist=array();
Preprocess($list[$i], $sent[$i],$shortlist);
$array = array();
for ($j=0; $j<count($shortlist); $j++) {
$array[] = $j;
}
$combos = explode('|',combos($array,''));
foreach ($combos as $seq) {
$new = Resequence($shortlist,$seq);
Process(1,$new, $sent[$i], $resultWords, $resultCount);
Process(2,$new, $sent[$i], $resultWords, $resultCount);
}
// take best result
$hi = max($resultCount);
$resultCount = array_flip($resultCount);
$best = $resultWords[$resultCount[$hi]];
usort($best,'bylen');
$resStr = '';
foreach($best as $b)
$resStr .= $b['output'].' ';
$finalCount += $hi;
$finalResult .= $resStr . '<br>';
}
exit("$finalResult$finalCount");
?>
</body>
</html>
Back to results
|
|