有一个下面的list<string>,通过一个算法使最终输出"*MLCQ-13463-120627-001*",
 
规则:选择次数出现最多的并且不能是"?"的,有没有不适用遍历的,最好LINQ能解决"*???"
"*MLC?-??"
"*MLCQ-??"
"*MLCQ?1346?+"
"*MLCQ?13463-?????"
"*MLCQ-13463-1206?"
"*MLCQ-13463-120627-??/"
"*MLCQ-13463-120627-001?"
"*?LCQ-?3463-120627-0?1*"
"*MLCQ-?3463-1?0627-001*"
 "*MLCQ-?3463-1?0627-001*"
 "*MLCQ-?3463-1?0627-001*"
 "*MLCQ-?3463-1?0?27-00??"
 "*MLCQ-?3463-1?0627-001?"
 "*MLCQ-?3463-1?0627-001?"
 "*MLCQ-?3463-1?06?7-00??"
 "*MLCQ-?3463-1?06?7-001?"
 "*ML?Q-??46?-1?06?7-001?"
 "*MLCQ-13463-1?0627-00??"
 "*MLCQ-13463-1?0627-00??"
 "*MLCQ-?346?-1?06??-00??"
 "*ML?Q-??46?-??06?7-001?"
 "*ML?Q-1346?-1?06?7-001?"
 "*ML?Q-1346?-1?06?7-00??"
 "*ML?Q-??463-1?06??-00??"
 "*ML?Q-1?463-1?06?7-001?"
 "*ML?Q-1?463-1?06?7-001?"
 "*?Z??"
 "*ML?Q-1?463-1?06?7-001?"
 "*M??Q-??46?-1?06??-00??"
 "*ML?Q-13463-1?06??-0???"
 "*ML?Q-13463-1?06??-0???"
 "*M??Q-?3463-1?06??-00?*"
 "*ML?Q-13463-1?06??-001*"
 "*M??Q-13463-1?06??-001*"
 "*M??Q-13463-1?06??-001*"
 "*M????13463-1?06??-0?1*"
 "*M??Q?13463-1206??-0?1?"
 "*M?CQ?13463-1206??-001*"
 "*MLCQ?13463-1206??-001*"
 "*MLC??13463-1206??-001*"
 "*M?CQ?13463-12062?-001*"
 "*MLCQ?13463-12062?-001*"
 "*MLCQ?13463-12062?-001*"
 "*MLCQ-13463-1206??-001*"
 "*MLCQ-13463-12062?-001*"

解决方案 »

  1.   

    string result=list.GroupBy(a => a).OrderByDescending(a => a.Count()).FirstOrDefault().Key.Replace("?",string.Empty);
                    //*MLCQ-3463-10627-001*
      

  2.   


                System.IO.StreamReader reader = new System.IO.StreamReader("e:\\1.txt");
                //下面对数据进行处理,分离出4个list
                List<string>[] list = new List<string>[4];
                list[0] = new List<string>();
                list[1] = new List<string>();
                list[2] = new List<string>();
                list[3] = new List<string>();
                while (!reader.EndOfStream)
                {
                    string[] s = reader.ReadLine().Split('-');
                    for (int i = 0; i < s.Length; i++)
                    {
                        list[i].Add(s[i]);
                    }
                }
                var vs = list[0].GroupBy(x => x).Select(g => new { g.Key, count = g.Count() }).Where(w => w.Key.Contains("?") == false).Max(c => c.Key) + "-" +
                    list[1].GroupBy(x => x).Select(g => new { g.Key, count = g.Count() }).Where(w => w.Key.Contains("?") == false).Max(c => c.Key) + "-" +
                    list[2].GroupBy(x => x).Select(g => new { g.Key, count = g.Count() }).Where(w => w.Key.Contains("?") == false).Max(c => c.Key) + "-" +
                    list[3].GroupBy(x => x).Select(g => new { g.Key, count = g.Count() }).Where(w => w.Key.Contains("?") == false).Max(c => c.Key);
                MessageBox.Show(vs.ToString());
    结果
    *MLCQ-13463-120627-001*
      

  3.   


    最后为什么要replace呢?还有先包含有?的排除掉 再分组排序 应该会快点把~
      

  4.   

    *MLCQ-13463-120627-001* 也就出现了一次吧
      

  5.   

    简单的例子,要是只含2条记录
    "*MLCQ-13463-120627-001?"
    "*?LCQ-?3463-120627-0?1*"
    输出的结果是:
    *MLCQ-13463-120627-001*
      

  6.   

    哈哈,其实你不是想要得到一个固定的值——"*MLCQ-13463-120627-001*",而是要得到出现最多次数的字符组合,你的第一行话误导了一片。
    不过你要不通过循环是不可能的,即使用Linq,还是有循环在里面,而且Linq的循环次数比你手控循环次数更多,因此Linq慢。
      

  7.   

    List<string> list = new List<string>() { 
                        "*???",
                        "*MLC?-??",
                        "*MLCQ-??",
                        "*MLCQ?1346?+",
                        "*MLCQ?13463-?????",
                        "*MLCQ-13463-1206?",
                        "*MLCQ-13463-120627-??/",
                        "*MLCQ-13463-120627-001?",
                        "*?LCQ-?3463-120627-0?1*",
                        "*MLCQ-?3463-1?0627-001*",
                         "*MLCQ-?3463-1?0627-001*",
                         "*MLCQ-?3463-1?0627-001*",
                         "*MLCQ-?3463-1?0?27-00??",
                         "*MLCQ-?3463-1?0627-001?",
                         "*MLCQ-?3463-1?0627-001?",
                         "*MLCQ-?3463-1?06?7-00??",
                         "*MLCQ-?3463-1?06?7-001?",
                         "*ML?Q-??46?-1?06?7-001?",
                         "*MLCQ-13463-1?0627-00??",
                         "*MLCQ-13463-1?0627-00??",
                         "*MLCQ-?346?-1?06??-00??",
                         "*ML?Q-??46?-??06?7-001?",
                         "*ML?Q-1346?-1?06?7-001?",
                         "*ML?Q-1346?-1?06?7-00??",
                         "*ML?Q-??463-1?06??-00??",
                         "*ML?Q-1?463-1?06?7-001?",
                         "*ML?Q-1?463-1?06?7-001?",
                         "*?Z??",
                         "*ML?Q-1?463-1?06?7-001?",
                         "*M??Q-??46?-1?06??-00??",
                         "*ML?Q-13463-1?06??-0???",
                         "*ML?Q-13463-1?06??-0???",
                         "*M??Q-?3463-1?06??-00?*",
                         "*ML?Q-13463-1?06??-001*",
                         "*M??Q-13463-1?06??-001*",
                         "*M??Q-13463-1?06??-001*",
                         "*M????13463-1?06??-0?1*",
                         "*M??Q?13463-1206??-0?1?",
                         "*M?CQ?13463-1206??-001*",
                         "*MLCQ?13463-1206??-001*",
                         "*MLC??13463-1206??-001*",
                         "*M?CQ?13463-12062?-001*",
                         "*MLCQ?13463-12062?-001*",
                         "*MLCQ?13463-12062?-001*",
                         "*MLCQ-13463-1206??-001*",
                         "*MLCQ-13463-12062?-001*"                };
                    StringBuilder sb = new StringBuilder();
                    int column_count = list.Max(a => a.Length);
                    string[] temp_arr = new string[column_count];
                    for (int j = 0; j < list.Count(); j++)
                    {
                        for (int k = 0; k < list[j].Length; k++)
                        {
                            temp_arr[k] += list[j][k];
                        }
                    }
                    foreach (string item in temp_arr)
                    {
                        var temp=item.ToCharArray().Where(a=>a!='?').GroupBy(a => a).OrderByDescending(a=>a.Count()).FirstOrDefault();
                        if (temp != null)
                        {
                            sb.Append(temp.Key.ToString());
                        }
                        else
                        {
                            sb.Append("?");
                        }
                    }
                    string result = sb.ToString();
                    //*MLCQ-13463-120627-001*
      

  8.   

        public static class SearchString
        {
            public static List<string> Search(this List<string> self,string str)
            {
                List<string> templist = new List<string>();
                string temp = string.Empty;
                int star = 0;
                int end = 0;
                foreach (char s in str)
                {
                    if(s.Equals('?'))
                    {
                        templist = self.Where(p => p.Substring(star, end) == temp).ToList();                    temp = string.Empty;
                        star = end;
                        continue;
                    }
                    temp += s;
                    end++;
                }            return null;
            }
        }