Problem: Use your most hands-on programming language to implement a function that prints all possible combinations of the characters in a string. These combinations range in length from one to the length of the string. Two combinations that differ only in ordering of the characters ARE the same combination. In other words, “12” and “31” are different combinations from the input string “123”, but “21 is the same as “12”.
For example, if we use “wxyz” as parameter for Combination(“wxyz”) the function will print out
w
x
y
z
wx
wy
wz
xy
xz
yz
wxy
wxz
wyz
xyz
wxyz




解为:
class Program
    {
        static void Main(string[] args)
        {
            string src_str;

            Console.WriteLine("请输入一个字符串,不要有空格:");
            src_str = Console.ReadLine();

            try
            {
                Console.WriteLine("可能的组合如下:");
                foreach (string str in CombineString(src_str))
                {
                    Console.WriteLine(str);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static ArrayList CombineString(string source_string)
        {
            ArrayList ret_list = new ArrayList();
            List<LengthChar> lenght_char_list = ParseString(source_string);

            for (int i = 1; i <= source_string.Length; i++)
            {
                ArrayList t_list = CombineStringHelper("", i, lenght_char_list, 0);

                if (t_list != null)
                {
                    ret_list.AddRange(t_list);
                }
            }

            return ret_list;
        }

        /// <summary>
        /// 分解为带数量的字符表。
        /// </summary>
        /// <param name="source_string"></param>
        /// <returns></returns>
        private static List<LengthChar> ParseString(string source_string)
        {
            Hashtable char_table = new Hashtable(0);
            List<LengthChar> lenght_char_list = new List<LengthChar>();

            foreach (char ch in source_string)
            {
                if (ch == ' ')
                {
                    throw new Exception("不能有空格!");
                }

                if (char_table.ContainsKey(ch))
                {
                    char_table[ch] = (int)char_table[ch] + 1;
                }
                else
                {
                    char_table[ch] = 1;
                }
            }

            foreach (DictionaryEntry de in char_table)
            {
                for (int i = 1; i <= (int)de.Value; i++)
                {
                    lenght_char_list.Add(new LengthChar((char)de.Key, i));
                }
            }

            return lenght_char_list;
        }

        /// <summary>
        /// 字符串组合辅助函数。
        /// </summary>
        /// <param name="base_str">基础字符串。</param>
        /// <param name="length">当前需要的字符串长度。</param>
        /// <param name="lenght_char_list">带数量标记的字符表。</param>
        /// <param name="index">当前字符表的起点。</param>
        /// <returns>组合好的字符串列表。</returns>
        private static ArrayList CombineStringHelper(string base_str, int length,

List<LengthChar> lenght_char_list, int index)
        {
            ArrayList ret_list = new ArrayList(0);

            if (base_str != string.Empty)
            {
                char last_ch = base_str[base_str.Length - 1];
                while (index < lenght_char_list.Count)
                {
                    if (last_ch != lenght_char_list[index].Ch)
                    {
                        break;
                    }
                    index++;
                }

                if (index == lenght_char_list.Count)
                {
                    return null;
                }
            }

            while (index < lenght_char_list.Count)
            {
                if (lenght_char_list[index].Length == length)
                {
                    ret_list.Add(base_str + lenght_char_list[index].ChStr);
                }
                else if (lenght_char_list[index].Length < length)
                {
                    ArrayList str_list = CombineStringHelper(base_str + lenght_char_list

[index].ChStr, length - lenght_char_list[index].Length,
                        lenght_char_list, index);

                    if (str_list != null)
                    {
                        ret_list.AddRange(str_list);
                    }
                }

                index++;
            }

            return ret_list;
        }
    }

    class LengthChar
    {
        private char ch;
        private int l;

        public LengthChar(char c, int length)
        {
            ch = c;
            l = length;
        }

        public string ChStr
        {
            get
            {
                string r = "";

                for (int i = 0; i < l; i++)
                {
                    r += ch;
                }

                return r;
            }
        }

        public char Ch
        {
            get
            {
                return ch;
            }
        }

        public int Length
        {
            get
            {
                return l;
            }
        }
    }

posted on 2006-06-09 00:27  nacarat  阅读(199)  评论(0)    收藏  举报