此示例把安東尼Pegram的想法轉換成代碼。我對你的字母映射和輸入進行了硬編碼,但你可以很容易地改變它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SO5672236
{
static class Program
{
static void Main()
{
// Setup your letter mappings first
Dictionary<char,string[]> substitutions = new Dictionary<char, string[]>
{
{'h', new[] {"h", "H"}},
{'e', new[] {"e", "E", "3"}},
{'l', new[] {"l", "L", "1"}},
{'o', new[] {"o", "O"}}
};
// Take your input
const string input = "hello";
// Get mapping for each letter in your input
IEnumerable<string[]> letters = input.Select(c => substitutions[c]);
// Calculate cortesian product
var cartesianProduct = letters.CartesianProduct();
// Concatenate letters
var result = cartesianProduct.Select(x => x.Aggregate(new StringBuilder(), (a, s) => a.Append(s), b => b.ToString()));
// Print out results
result.Foreach(Console.WriteLine);
}
// This function is taken from
// http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] { item }));
}
// This is a "standard" Foreach helper for enumerables
public static void Foreach<T>(this IEnumerable<T> enumerable, Action<T> action)
{
foreach (T value in enumerable)
{
action(value);
}
}
}
}
謝謝你,偉大的答案/評論 - 不幸我不能投票了ATM – tanxiong 2011-04-15 04:32:50
@tanxiong:如果這是足以讓你開始,你可以接受的答案。 – 2011-04-15 04:40:22