代码改变世界

处理一个字符串成为一个特定规则的表达式

2011-07-01 22:56  音乐让我说  阅读(209)  评论(0编辑  收藏  举报

算法题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConAppTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string words = "1-1,1-2,1-3,5-3,4-1,5-1,5-2,4-6,4-5";
            //需要得到的结果为:{[1(1-3)][5(1-3)][4(1-1)(5-6)]}
            //规则:如果连续,比如 1,2,3,则用 1-3 表示。如果不连续,则用 X-X 表示,比如 5-5
            string result = HandleString(words);
            Console.WriteLine(result);
            Console.ReadKey();
        }

        static string HandleString(string words)
        {
            if (string.IsNullOrEmpty(words))
            {
                throw new ArgumentNullException("words");
            }
            string[] wordsArray = words.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            Dictionary<int, List<int>> dicContainer = new Dictionary<int, List<int>>();
            
            string[] tempWordArray;
            int key;
            int value;
            foreach (string wordItem in wordsArray)
            {
                tempWordArray = wordItem.Split('-');
                if (tempWordArray.Length != 2)
                {
                    throw new ArgumentException("参数的格式不正确,比如是:Key-Value 格式");
                }
                key = int.Parse(tempWordArray[0]);
                value = int.Parse(tempWordArray[1]);
                if (dicContainer.ContainsKey(key))
                {
                    List<int> ltValue = dicContainer[key];
                    if (!ltValue.Contains(value))
                    {
                        ltValue.Add(value);
                    }
                }
                else
                {
                    dicContainer[key] = new List<int>()
                    {
                        value
                    };
                }
            }
            //以下注释是调试信息
            //foreach (int keyItem in dicContainer.Keys)
            //{
            //    Console.Write("Key:" + keyItem + ",Value:");
            //    foreach (int valueItem in dicContainer[keyItem])
            //    {
            //        Console.Write(valueItem + ",");
            //    }
            //    Console.WriteLine("");
            //}
            Console.WriteLine("");
            StringBuilder result = new StringBuilder("{");
            int minValue;
            int maxValue;
            int cursorValue;
            bool flag;
            foreach (int keyItem in dicContainer.Keys)
            {
                result.Append("[" + keyItem);
                List<int> ltValue = new List<int>(dicContainer[keyItem]);
                while(ltValue.Count > 0)
                {
                    minValue = ltValue.Min();
                    ltValue.Remove(minValue);
                    cursorValue = maxValue = minValue;
                    flag = true;
                    do
                    {
                        cursorValue++;
                        if (ltValue.Contains(cursorValue))
                        {
                            maxValue = cursorValue;
                            ltValue.Remove(maxValue);
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    } while (flag);
                    result.Append("(" + minValue + "-" + maxValue + ")");
                }
                result.Append("]");
            }
            result.Append("}");
            return result.ToString();
        }
    }

    
}

谢谢浏览!