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

[200分]求高效并集算法
有2串类似的字符串表示2个集合.

5|5:1|4:1|3:1|2:0|1:1;4|5:1|4:1|1:0;3|4:0|2:1|1:1;2|2:1|1:0;1|5:1|3:0
5|5:0|3:1|2:1;4|4:1;3|3:1;2|2:1;1|5:1|4:0

其中字符串用";"可以分割成

5|5:1|4:1|3:1|2:0|1:1
;
4|5:1|4:1|1:0
;
3|4:0|2:1|1:1
;
2|2:1|1:0
;
1|5:1|3:0

以2|2:1|1:0举例:以"|"分割 第一个2表示组名,后面2:1中2表示组员,1表示存在;1:0中1表示组员,0表示不存在.
(为什么不存在还要留下用0表示呢,因为这表示它曾经存在过- -!)
现在这2个集合求并集,
要求如下
例如集合 1|1:0|2:1;2|1:1|2:0 
  和集合 1|1:1|2:0;3|1:1 的并集
  结果为 1|1:1|2:1;2|1:1|2:0;3|1:1

C# code

        public Hashtable GetGroup(string Source)
        {
            Hashtable Group = new Hashtable { };
            string[] GroupArr = Source.Split(';'); //根据字符串分组
            Array.ForEach(GroupArr, group =>
            {
                if (!string.IsNullOrEmpty(group))
                {
                    string[] groupArr = group.Split('|'); //根据字符串分组员
                    Hashtable Element = new Hashtable { };
                    for (int i = 1; i < groupArr.Count(); i++)
                    {
                        if (!string.IsNullOrEmpty(groupArr[i]))
                        {
                            string[] ElementArr = groupArr[i].Split(':'); //根据字符串分组员状态
                            Element.Add(int.Parse(ElementArr[0]), ElementArr[1]); 
                        }
                    }
                    Group.Add(int.Parse(groupArr[0]), Element);
                }
            });
            return Group;
        }


现在我是把它分割然后装在HashTabel里,然后在进行对比合并,感觉不是很理想.

不知道哪位大大还有更好的方法吗?谢谢~


------解决方案--------------------
例如集合 1|1:0|2:1;2|1:1|2:0
和集合 1|1:1|2:0;3|1:1 的并集
结果为 1|1:1|2:1;2|1:1|2:0;3|1:1


规则是?
------解决方案--------------------
C# code

 public List<List<string>> GetGroup()
        {
            string Source1 = "5|5:1|4:1|3:1|2:0|1:1;4|5:1|4:1|1:0;3|4:0|2:1|1:1;2|2:1|1:0;1|5:1|3:0";
            string Source2 = "5|5:0|3:1|2:1;4|4:1;3|3:1;2|2:1;1|5:1|4:0";

            List<List<string>> Group = new List<List<string>>();
            string[] GroupArr = Source1.Split(';'); //根据字符串分组
            var sp1 = Source1.Split(new char[] { ';', '|' });
            var sp2 = Source2.Split(new char[] { ';', '|' });
            var sp3 = sp1.Concat(sp2).Distinct().ToList();          
            Group.Add(sp3);
            //Array.ForEach(GroupArr, group =>
            //{
            //    if (!string.IsNullOrEmpty(group))
            //    {
            //        string[] groupArr = group.Split('|'); //根据字符串分组员
            //        Hashtable Element = new Hashtable { };
            //        for (int i = 1; i < groupArr.Count(); i++)
            //        {
            //            if (!string.IsNullOrEmpty(groupArr[i]))
            //            {
            //                string[] ElementArr = groupArr[i].Split(':'); //根据字符串分组员状态
            //                Element.Add(int.Parse(ElementArr[0]), ElementArr[1]);
            //            }
            //        }
            //        Group.Add(int.Parse(groupArr[0]), Element);
            //    }
            //});
            return Group;
        }

------解决方案--------------------
一看这个分数就知道我只能是进来学习的。。。
------解决方案--------------------
探讨
引用:

C# code

public List<List<string>> GetGroup()
{
string Source1 = "5|5:1|4:1|3:1|2:0|1:1;4|5:1|4:1|1:0;3|4:0|2:1|1:1;2|2:1|1:0;1|5:1|3:0";
string Source2 = "5|5:0|3:1……