如何消除与字谜结果相等或相反的字符串?
Posted
技术标签:
【中文标题】如何消除与字谜结果相等或相反的字符串?【英文标题】:How to eliminate strings that are equal to or the reverse of an anagram result? 【发布时间】:2013-06-19 21:23:05 【问题描述】:我已经接近我想要的了。我遇到了一些逻辑问题,如果结果等于或与所述结果相反,则不显示结果。我正在寻找两个美国州名的字谜,它们是其他两个州名的字谜。我找到了解决方案,现在它真的只是一个显示问题........我想。任何帮助将不胜感激。谢谢。
<?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>";
?>
【问题讨论】:
【参考方案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);
【讨论】:
这清理得很好,谢谢。不过,其他比赛呢?它似乎停止得太早了。例如...南达科他州和北卡罗来纳州? 是的,南达科他州/北卡罗来纳州 - 北达科他州/南卡罗来纳州是字谜 好的,再次感谢。我花了相当多的时间试图获取每一种可能性的结果(包括原始字符串并且它是反向的),将它们放入一个数组中 - 然后过滤掉重复项。不用说,我还没有让它工作。以上是关于如何消除与字谜结果相等或相反的字符串?的主要内容,如果未能解决你的问题,请参考以下文章