我已经接近了我想要的东西。我遇到了一些逻辑问题,如果它们与所述结果相等或相反,则不显示结果。我正在寻找两个美国州名的字谜,这是两个其他州名的字谜。我找到了解决方案,现在它只是一个显示问题........我想。任何帮助将不胜感激。谢谢。
<?php
ini_set('max_execution_time', 300);
function anagramOf ($array, $start, $len)
{
global $i;
$word = implode ("", $array);
$i = ++$i;
printf ("%'.-20d%'.20s%s", $i, ucfirst($word), "<BR> \n");
}
function sortstr ($astring)
{
global $word, $s, $l;
$s = strtolower ($astring);
$array = preg_split ('//', $s, -1, PREG_SPLIT_NO_EMPTY);
sort ($array);
$s = implode ("", $array);
}
function search ($aword,$pairs)
{
global $len, $word, $s, $i;
$size = count ($pairs);
for ($k = 0; $k < $size; $k++)
{
$pairs[$k] = chop ($pairs[$k]);
if (strlen ($pairs[$k]) == $len)
{
$l = $pairs[$k];
sortstr ($l);
$ls = $s;
if (!strcmp ($ls, $word))
{
$i = ++$i;
printf ("%'.-20d%'.20s%s", $i, ucfirst ($l), "</br</br>");
}
}
}
}
$states1 = array("Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming");
$states2 = array();
$states2 = $states1;
$pairs = array();
$countS1 = count($states1);
$countS2 = count($states2);
$countPr;
$i=0;
foreach($states1 as $s1array)
{
$j=0;
foreach($states2 as $s2array)
{
$pairs[]=array($s1array => $s2array);
$j++;
}
$i++;
}
$pCountPair = count($pairs);
$newpairs = array();
$l=0;
foreach($pairs as $p => $pairs2)
{
foreach($pairs2 as $name1 => $name2)
{
$newpairs[]=$name1.$name2;;
}
$l++;
}
$p2count = count($newpairs);
#echo 'p2 = '.$p2count.'</br>';
for($r = 0; $r < $p2count; $r++)
{
$word = $newpairs[$r];
echo "Anagrams of <b>".ucfirst ($word)."</b> :\n<BR>\n";
$na = $word;
sortstr ($word);
$word = $s;
$len = strlen ($word);
$i = 0;
search ($word,$newpairs);
}
if ($i == 0)
echo "Can't find any anagram of <B>".ucfirst($na)."</B> in the pairs array.</br></br>";
?>
答案 0 :(得分:1)
所以最大的问题是你们一起加入各州的方式。在您匹配的州名单中,您将匹配AlabamaAlaska和AlaskaAlabama。所以要做的好事只是匹配唯一的两种状态组合。
$doubleStates = array();
for ($i = 0, $max = count($states); $i < $max; $i++) {
// The first loop starts from the first element of the array
for ($j = $i + 1; $j < $max; $j++) {
// The second loop starts from the next element of the first loop
$doubleStates[] = $states[$i] . $states[$j];
}
}
我可能不应该为你编写所有代码但是我被带走了:
<?php
function findAnagrams($array) {
do {
$matcher = null;
$matches = array();
foreach ($array as $key => $val) {
if (is_null($matcher)) {
unset($array[$key]);
$matcher = $val;
} elseif (isAnagram($matcher, $val)) {
if (empty($matches)) {
$matches[] = $matcher;
}
$matches[] = $val;
}
}
if (count($matches) > 0) {
print_r($matches);
echo "<br >";
}
} while (count($array) > 0);
}
function isAnagram($a, $b) {
return letterize($a) == letterize($b);
}
function letterize($a) {
$array = str_split(str_replace(' ', '', strtolower($a)));
sort($array);
return join($array);
}
$states = array('Alabama','Alaska','Arizona','Arkansas','California','Colorado',
'Connecticut','Delaware','Florida','Georgia','Hawaii','Idaho','Illinois',
'Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland',
'Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana',
'Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York',
'North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania',
'Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah',
'Vermont','Virginia','Washington','West Virginia','Wisconsin','Wyoming');
$doubleStates = array();
for ($i = 0, $max = count($states); $i < $max; $i++) {
for ($j = $i + 1; $j < $max; $j++) {
$doubleStates[] = $states[$i] . $states[$j];
}
}
findAnagrams($doubleStates);