算法的问题:字母组合 [英] Algorithm Issue: letter combinations
问题描述
我想写一张code,这将做到以下几点:
I'm trying to write a piece of code that will do the following:
以数字0到9,并指定一个或多个字母到这个号码。例如:
Take the numbers 0 to 9 and assign one or more letters to this number. For example:
0 = N,
1 = L,
2 = T,
3 = D,
4 = R,
5 = V or F,
6 = B or P,
7 = Z,
8 = H or CH or J,
9 = G
当我有一个code像0123,这是轻而易举的EN code吧。这显然构成了code NLTD。当一些像5,6或8的介绍,事情变得不同。一些像051会导致多种可能:
When I have a code like 0123, it's an easy job to encode it. It will obviously make up the code NLTD. When a number like 5,6 or 8 is introduced, things get different. A number like 051 would result in more than one possibility:
NVL和NFL
这应该是显而易见的,这变得更加雪上加霜较长的数字,其中包括几位像5,6或8。
It should be obvious that this gets even "worse" with longer numbers that include several digits like 5,6 or 8.
作为pretty的坏的数学,我还没有能够拿出一个体面的解决办法,让我养活程序一串数字,让它吐出所有可能的字母组合。所以,我喜欢一些帮助吧,因为我似乎无法找到答案。挖出了关于排列组合的一些信息,但没有运气。
Being pretty bad at mathematics, I have not yet been able to come up with a decent solution that will allow me to feed the program a bunch of numbers and have it spit out all the possible letter combinations. So I'd love some help with it, 'cause I can't seem to figure it out. Dug up some information about permutations and combinations, but no luck.
感谢您的任何建议/线索。语言我需要写code。在PHP,但任何一般的提示将是非常美联社preciated。
Thanks for any suggestions/clues. The language I need to write the code in is PHP, but any general hints would be highly appreciated.
一些更多的背景:(!和非常感谢的快速反应)
Some more background: (and thanks a lot for the quick responses!)
后面我的问题的想法是建立一个脚本,将帮助人们轻松地转换他们要记住那些更容易记住单词数。这有时被称为伪命理
The idea behind my question is to build a script that will help people to easily convert numbers they want to remember to words that are far more easily remembered. This is sometimes referred to as "pseudo-numerology".
我想剧本给我的一切,都是那么反对剥夺字的数据库中保存了可能的组合。这些剥离的话刚刚从字典和有我在我的问题剥离出来,其中提到的字母。这样一来,被连接$ C $光盘通常可以很容易地与一个或多个数据库记录的数量。当发生这种情况,你结了,你可以用它来记住你想记住一些单词的列表。
I want the script to give me all the possible combinations that are then held against a database of stripped words. These stripped words just come from a dictionary and have all the letters I mentioned in my question stripped out of them. That way, the number to be encoded can usually easily be related to a one or more database records. And when that happens, you end up with a list of words that you can use to remember the number you wanted to remember.
推荐答案
您想握住你的电话号码的一般结构 - >号的分配是一个数组或数组,类似于:
The general structure you want to hold your number -> letter assignments is an array or arrays, similar to:
// 0 = N, 1 = L, 2 = T, 3 = D, 4 = R, 5 = V or F, 6 = B or P, 7 = Z,
// 8 = H or CH or J, 9 = G
$numberMap = new Array (
0 => new Array("N"),
1 => new Array("L"),
2 => new Array("T"),
3 => new Array("D"),
4 => new Array("R"),
5 => new Array("V", "F"),
6 => new Array("B", "P"),
7 => new Array("Z"),
8 => new Array("H", "CH", "J"),
9 => new Array("G"),
);
随后,有点递归的逻辑,给了我们类似的功能:
Then, a bit of recursive logic gives us a function similar to:
function GetEncoding($number) {
$ret = new Array();
for ($i = 0; $i < strlen($number); $i++) {
// We're just translating here, nothing special.
// $var + 0 is a cheap way of forcing a variable to be numeric
$ret[] = $numberMap[$number[$i]+0];
}
}
function PrintEncoding($enc, $string = "") {
// If we're at the end of the line, then print!
if (count($enc) === 0) {
print $string."\n";
return;
}
// Otherwise, soldier on through the possible values.
// Grab the next 'letter' and cycle through the possibilities for it.
foreach ($enc[0] as $letter) {
// And call this function again with it!
PrintEncoding(array_slice($enc, 1), $string.$letter);
}
}
三呼万岁的递归!这将通过使用:
Three cheers for recursion! This would be used via:
PrintEncoding(GetEncoding("052384"));
如果你真的想作为一个数组,玩输出缓冲和爆炸使用\ N作为您的分割字符串。
And if you really want it as an array, play with output buffering and explode using "\n" as your split string.
这篇关于算法的问题:字母组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!