日期:2014-05-18  浏览次数:21078 次

将英文单词分解成字母组合,求解!
我想将英文单词分解成任意的1个、2个和3个字母的组合,不同的组合之间用逗号分开。比如,单词CSDN可以分解成"C,S,D,N"(由4个单个字母组成)、"CS,DN"(分解为两个两个字母组合)、"CS,D, N"(一个两个字母组合+两个一个字母组合)、"C,SD,N"、"C,S,DN"、"CSD,N" (一个三个字母组合+1个1个字母组合)、"C,SDN"。不知道要怎样才能通过程序来穷尽这些组合方式,还请大家帮忙!

------解决方案--------------------
static void Main(string[] args)
{
string str = "1234";
var array = new System.Collections.Generic.List<int>();
for (int i = 0; i < str.Length; i++)
{
array.Add(0);
}
fun(str, 0, array);
foreach (string s in strarray)
Console.WriteLine(s);
Console.ReadLine();
}
static System.Collections.Generic.List<string> strarray = new List<string>();
static void fun(string str, int n, System.Collections.Generic.List<int> array)
{
if (n == str.Length-1)
{
string t = "";
for (int i = 0; i < str.Length; i++)
{
t+=str[i];
if (i<str.Length-1&&array[i] != 0)
{
t+=",";
}
}
if (!strarray.Contains(t))
strarray.Add(t);
}
else if (n < str.Length)
{
for (int i = n + 1; i <= str.Length; i++)
{
array[i-1] = 1;
fun(str, i, array);
array[i-1] = 0;
}
for (int i =str.Length-1; i >n; i--)
{
array[i] = 1;
fun(str, i, array);
array[i] = 0;
}
}
}