Dictionary<string, int> dtsi = new Dictionary<string, int>();
            dtsi.Add("efghi", 28);
            dtsi.Add("opqrs", 26);
            dtsi.Add("aaabb", 25);
            dtsi.Add("defgh", 23);
            dtsi.Add("cdefg", 22);
            dtsi.Add("bcdef", 22);
            dtsi.Add("abcde", 16);
            dtsi.Add("cccdd", 13);
string 长度是一样的,int是由大到小排列的.
-------------------------------------------------------------
abcde
bcdef
cdefg
defgh
....   这些( 后面去掉一个 等于 另一条去掉前面一个) 的一连串..被视为重复.这样的只留他所在int最大的那条.最后结果要这样:
efghi 28   //只留这个最大的
opqrs 26
aaabb 25
cccdd 13

解决方案 »

  1.   

    从例子上看是Int重复,
    使用hashTable,
    如果Int重复,用Int做Key,如果string重复,用string做Key
      

  2.   

    呃...不是int重复-------------------------------
    bcdef 前面去掉一个得  cdef
    cdefg 前面去掉一个得  defg  后面去掉一个得cdef
    defgh 后面去掉一个得  defg
    则这几条是重复的.  只留int最大的.
    bcdef的int是18. 
    cdefg的int是20.
    defgh的int是19.  那么只留cdefg .去掉defgh和bcdef字符串里面可能有中文.
      

  3.   

    重复的规则 是 他们都是 bcdefgh切成的.所以 bcdef 前面去掉一个字 等于 cdefg 后面去掉一个字.都是cdef
    所以他们是重复的.到这里能理解吧?
    下面处理重复的.把重复集里面 int最大的留下.其他都去掉. 
    bcdef的int是18.  
    cdefg的int是20.
    defgh的int是19. 那么只留cdefg .去掉defgh和bcdef
    字符串里面可能有中文.
      

  4.   

    string里面4个连续相同按int值由大到小去重复排列?
      

  5.   


                Dictionary<string, int> dtsi = new Dictionary<string, int>();
                dtsi.Add("efghi", 28);
                dtsi.Add("opqrs", 26);
                dtsi.Add("aaabb", 25);
                dtsi.Add("defgh", 23);
                dtsi.Add("cdefg", 22);
                dtsi.Add("bcdef", 22);
                dtsi.Add("abcde", 16);
                dtsi.Add("cccdd", 13);            string key = "";
                string key1 = "";
                List<string> list = new List<string>();
                foreach (KeyValuePair<string, int> kv in dtsi)
                {
                    foreach (KeyValuePair<string, int> kv1 in dtsi)
                    {
                        if (kv.Key == kv1.Key) continue;
                        key = kv.Key.Substring(0, 4);
                        key1 = kv1.Key.Substring(1, 4);
                        if (key == key1)
                        {
                            if (!list.Contains(kv.Key))
                            {
                                list.Add(kv.Key);
                            }
                            if (!list.Contains(kv1.Key))
                            {
                                list.Add(kv1.Key);
                            }
                        }
                    }
                }
                foreach (string k in list)
                {
                    dtsi.Remove(k);
                }
                foreach (KeyValuePair<string, int> kv in dtsi)
                {
                    MessageBox.Show(kv.Key + "***" + kv.Value);
                }
      

  6.   


    你这个结果不对呀.."efghi", 28 都没了...
      

  7.   

    abcde
    bcdef
    cdefg
    defgh  为什么是重复?
    因为是由abcdefgh 这个字符串切成的..是连续的.这一堆只取int最大的一条.
    再不明白绝对是理解能力的问题.而不是表达能力的问题.
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, int> dtsi = new Dictionary<string, int>();
                dtsi.Add("efghi", 28);
                dtsi.Add("opqrs", 26);
                dtsi.Add("aaabb", 25);
                dtsi.Add("defgh", 23);
                dtsi.Add("cdefg", 22);
                dtsi.Add("bcdef", 22);
                dtsi.Add("abcde", 16);
                dtsi.Add("cccdd", 13);
                Grouping(dtsi).ForEach(x => 
                    Console.WriteLine(x.Key + "\t" + x.Value));
            }        static bool AsSame(string a, string b)
            {
                return a.Contains(b.Substring(0, b.Length - 1)) || 
                        a.Contains(b.Substring(1));
            }        static void ProcessItem(Dictionary<string, int> dict, KeyValuePair<string, int> item)
            {
                if (dict.Where(x => AsSame(x.Key, item.Key)).Count() > 0) 
                    if (!dict.ContainsKey(item.Key))
                        dict.Add(item.Key, item.Value);
            }        static List<KeyValuePair<string, int>> Grouping(Dictionary<string, int> dict)
            {
                List<Dictionary<string, int>> list = new List<Dictionary<string, int>>();
                Dictionary<string, int> di;
                dict.ToList().ForEach(x => { di = new Dictionary<string,int>(); di.Add(x.Key, x.Value); list.Add(di); });
                list.ForEach(x => dict.ToList().ForEach(y => ProcessItem(x, y)));
                list.ForEach(x => x = x.OrderBy(y => y.Key).ToDictionary(y => y.Key, y => y.Value));
                Dictionary<string, int>[] arr = list.ToArray();
                for (int j = 0; j < arr.GetLength(0); j++)
                {
                    var items = list.Where(x => x.Count > arr[j].Count).ToArray();
                    for (int i = 0; i < items.Count(); i++)
                    {
                        if (items[i].Except(arr[j]).Count() == items[i].Count - arr[j].Count)
                            list.Remove(arr[j]);
                    }
                }
                var r = list.Select(x => x.OrderByDescending(y => y.Value).First());
                return r.Distinct().ToList();
            }
        }
    }efghi   28
    opqrs   26
    aaabb   25
    cccdd   13
    Press any key to continue . . .
      

  9.   

    caozhy兄真的谢谢你又来帮忙..不过你的好像不对..
                Dictionary<string, int> dtsi = new Dictionary<string, int>();
                dtsi.Add("abcde", 31);
                dtsi.Add("efghi", 28);
                dtsi.Add("opqrs", 26);
                dtsi.Add("aaabb", 25);
                dtsi.Add("defgh", 23);
                dtsi.Add("cdefg", 22);
                dtsi.Add("bcdef", 22);
                dtsi.Add("cccdd", 13);
    把abcde到上面就不对了.
      

  10.   

    本帖最后由 caozhy 于 2011-06-21 17:41:02 编辑
      

  11.   

                Dictionary<string, int> dtsi = new Dictionary<string, int>();
                dtsi.Add("abcde", 31);
                dtsi.Add("efghi", 31);
                dtsi.Add("opqrs", 26);
                dtsi.Add("aaabb", 25);
                dtsi.Add("defgh", 23);
                dtsi.Add("cdefg", 22);
                dtsi.Add("bcdef", 22);
                dtsi.Add("cccdd", 13);
    如果int也重复只留最靠上一个可以么.代码我先用着哈..回头有时间再来学习.
      

  12.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class MyComparer : IEqualityComparer<KeyValuePair<string, int>>
        {
            public bool Equals(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
            {
                return x.Value == y.Value;
            }        public int GetHashCode(KeyValuePair<string, int> obj)
            {
                return obj.Value.GetHashCode();
            }
        }    class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, int> dtsi = new Dictionary<string, int>();
                dtsi.Add("abcde", 31);
                dtsi.Add("efghi", 31);
                dtsi.Add("opqrs", 26);
                dtsi.Add("aaabb", 25);
                dtsi.Add("defgh", 23);
                dtsi.Add("cdefg", 22);
                dtsi.Add("bcdef", 22);
                dtsi.Add("cccdd", 13);
                Grouping(dtsi).ForEach(x =>
                    Console.WriteLine(x.Key + "\t" + x.Value));
            }        static bool AsSame(string a, string b)
            {
                return a.Contains(b.Substring(0, b.Length - 1)) ||
                        a.Contains(b.Substring(1));
            }        static void ProcessItem(Dictionary<string, int> dict, KeyValuePair<string, int> item)
            {
                if (dict.Where(x => AsSame(x.Key, item.Key)).Count() > 0)
                    if (!dict.ContainsKey(item.Key))
                        dict.Add(item.Key, item.Value);
            }        static List<KeyValuePair<string, int>> Grouping(Dictionary<string, int> dict)
            {
                List<Dictionary<string, int>> list = new List<Dictionary<string, int>>();
                Dictionary<string, int> di;
                dict.ToList().ForEach(x => { di = new Dictionary<string, int>(); di.Add(x.Key, x.Value); list.Add(di); });
                for (int i = 0; i < dict.Count; i++)
                    list.ForEach(x => dict.ToList().ForEach(y => ProcessItem(x, y)));
                list.ForEach(x => x = x.OrderBy(y => y.Key).ToDictionary(y => y.Key, y => y.Value));
                Dictionary<string, int>[] arr = list.ToArray();
                for (int j = 0; j < arr.GetLength(0); j++)
                {
                    var items = list.Where(x => x.Count > arr[j].Count).ToArray();
                    for (int i = 0; i < items.Count(); i++)
                    {
                        if (items[i].Except(arr[j]).Count() == items[i].Count - arr[j].Count)
                            list.Remove(arr[j]);
                    }
                }
                var r = list.Select(x => x.OrderByDescending(y => y.Value).First());
                return r.Distinct(new MyComparer()).OrderByDescending(x => x.Value).ToList();
            }
        }
    }abcde   31
    opqrs   26
    aaabb   25
    cccdd   13
    Press any key to continue . . .
      

  13.   

    不好意思.是我逻辑思维不够严谨..一开始我给出的int是重复的.只是我没有想到重复的int要怎么处理..
    呃..你这样一调 不是重复的字串 int是31的 也没有了..
      

  14.   

      public static void fun(Dictionary<string, int> dtsi)
            {
                string key = "";
                string key1 = "";
                List<string> list = new List<string>();
                foreach (KeyValuePair<string, int> kv in dtsi)
                {
                    foreach (KeyValuePair<string, int> kv1 in dtsi)
                    {
                        if (kv.Key == kv1.Key) continue;
                        key = kv.Key.Substring(1);
                        key1 = kv1.Key.Substring(0, kv1.Key.Length - 1);
                        if (key == key1)
                        {
                            if (!list.Contains(kv1.Key) && kv.Value > kv1.Value)
                            {
                                list.Add(kv1.Key);
                            }
                            else
                            {
                                list.Add(kv.Key);
                            }
                        }
                    }
                }
                foreach (string k in list)
                {
                    dtsi.Remove(k);
                }
            }
    最后我自己用sunny906前辈的代码改了下好像也适合用..
      

  15.   

    是对dtsi本身的修改..而且前提是int是要一开始说的由大到小排列..
      

  16.   

    本帖最后由 caozhy 于 2011-06-21 20:01:40 编辑
      

  17.   

    static void Main(string[] args)
        {
            Dictionary<string, int> dtsi = new Dictionary<string, int>();
            dtsi.Add("efghi", 28);
            dtsi.Add("opqrs", 26);
            dtsi.Add("aaabb", 25);
            dtsi.Add("defgh", 23);
            dtsi.Add("cdefg", 22);
            dtsi.Add("bcdef", 22);
            dtsi.Add("abcde", 16);
            dtsi.Add("cccdd", 13);
            Dictionary<string, int> result = new Dictionary<string, int>();
            var ordered = dtsi.OrderByDescending(x => x.Value);
            foreach (var kvp in dtsi)
            {
                string a = kvp.Key.Substring(1);
                string b = kvp.Key.Substring(0,kvp.Key.Length-1);
                if (result.Any(x => x.Key.StartsWith(a) || x.Key.EndsWith(b)))
                    result.Add(kvp.Key, -1);
                else
                    result.Add(kvp.Key, kvp.Value);
            }        dtsi.Clear(); //这段代码只是为了满足“对dtsi本身修改”
            foreach (var item in result.Where(x => x.Value > 0))
            {
                dtsi.Add(item.Key, item.Value);
            }        foreach (var item in dtsi)
        {
                Console.WriteLine(item.Key + "/" + item.Value);
        }
            Console.Read();
        }
      

  18.   

    上面是用-1做标记。可以换成int? 和null
     
     static void Main(string[] args)
        {
            Dictionary<string, int> dtsi = new Dictionary<string, int>();
            dtsi.Add("efghi", 28);
            dtsi.Add("opqrs", 26);
            dtsi.Add("aaabb", 25);
            dtsi.Add("defgh", 23);
            dtsi.Add("cdefg", 22);
            dtsi.Add("bcdef", 22);
            dtsi.Add("abcde", 16);
            dtsi.Add("cccdd", 13);
            Dictionary<string, int?> temp = new Dictionary<string, int?>();
            var ordered = dtsi.OrderByDescending(x => x.Value);
            foreach (var kvp in dtsi)
            {
                string a = kvp.Key.Substring(1);
                string b = kvp.Key.Substring(0,kvp.Key.Length-1);
                if (temp.Any(x => x.Key.StartsWith(a) || x.Key.EndsWith(b)))
                    temp.Add(kvp.Key, null);
                else
                    temp.Add(kvp.Key, kvp.Value);
            }
            dtsi.Clear(); //这段代码只是为了满足“对dtsi本身修改”
            foreach (var item in temp.Where(x => x.Value != null))
            {
                dtsi.Add(item.Key, item.Value.Value);
            }
            foreach (var item in dtsi)
        {
                Console.WriteLine(item.Key + " " + item.Value);
        }
            Console.Read();
        }