首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

[200分]求高效并集算法解决思路

2012-04-14 
[200分]求高效并集算法有2串类似的字符串表示2个集合.5|5:1|4:1|3:1|2:0|1:14|5:1|4:1|1:03|4:0|2:1|1:1

[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……



用控制台试试看看时……

热点排行