问题描述
{3,a}{3,b}{2,e}{5,c}{6,d}{9,a}{6,e}想这样的数组有N个这样的一维数组我想做融合,意思就是有一样的就合并例如上面的融合后的结果这这个样子的{3,a,b,9}{2,e,6,d}{5,c}求解
解决方案
解决方案二:
只说思路。首先定义一个链表。从第一个元素开始操作,先把第一个元素从链表里删除,然后遍历链表,寻找跟此元素有关系的元素,找到之后跟之前的元素合并,并且把找到的元素从链表里删除,然后重头开始遍历。遍历结束后就是跟第一个元素相关的元素都找齐了,然后再从现在链表里的第一个元素开始重复前面的过程。时间复杂度没细算,应该是O(N!)
解决方案三:
List<string[]>list_result=newList<string[]>();List<string[]>list_test=newList<string[]>();list_test.Add(newstring[]{"3","a"});list_test.Add(newstring[]{"3","b"});list_test.Add(newstring[]{"2","e"});list_test.Add(newstring[]{"5","c"});list_test.Add(newstring[]{"6","d"});list_test.Add(newstring[]{"9","a"});list_test.Add(newstring[]{"6","e"});for(inti=0;i<list_test.Count();i++){List<string>temp_arr=newList<string>();foreach(vartemp_iteminlist_test[i]){temp_arr.Add(temp_item);}list_test.RemoveAt(i);for(;;){inttemp_count=temp_arr.Count();for(intj=0;j<list_test.Count();j++){List<string>pass_temp=list_test[j].ToList();if(pass_temp.Intersect(temp_arr).Count()>0){temp_arr=temp_arr.Concat(pass_temp.Except(pass_temp.Intersect(temp_arr))).ToList();list_test.RemoveAt(j);}}if(temp_count==temp_arr.Count())break;}list_result.Add(temp_arr.ToArray());}/*list_resultCount=3System.Collections.Generic.List<string[]>[0]{string[4]}string[][0]"3"string[1]"a"string[2]"b"string[3]"9"string[1]{string[2]}string[][0]"5"string[1]"c"string[2]{string[4]}string[][0]"6"string[1]"e"string[2]"2"string[3]"d"string*/
解决方案四:
谢谢你们
解决方案五:
该回复于2012-04-18 09:50:08被版主删除
解决方案六:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){varlist=newList<string[]>(){newstring[]{"3","a"},newstring[]{"3","b"},newstring[]{"2","e"},newstring[]{"5","c"},newstring[]{"6","d"},newstring[]{"9","a"},newstring[]{"6","e"}};varMaps=newList<List<string>>();list.ForEach(x=>{if(Maps.Any(y=>y.Any(z=>x.Contains(z))))Maps.Where(y=>y.Any(z=>x.Contains(z))).First().AddRange(x);elseMaps.Add(newList<string>(x));});Maps.ForEach(x=>{if(Maps.Where(y=>y!=x).Any(y=>y.Any(z=>x.Contains(z)))){Maps.Where(y=>y!=x).Where(y=>y.Any(z=>x.Contains(z))).First().AddRange(x);x.Clear();}});foreach(variteminMaps.Where(x=>x.Count>0)){Console.WriteLine(string.Join(",",item.Distinct()));}}}}
3,a,b,95,c6,d,2,ePressanykeytocontinue...
解决方案七:
你可以看我这个帖子,打散后差不多http://topic.csdn.net/u/20120409/00/12bdc8f7-6f3e-43c4-bf43-86feaf07e47c.html
解决方案八:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){varlist=newList<string[]>(){newstring[]{"3","a"},newstring[]{"3","b"},newstring[]{"2","e"},newstring[]{"5","c"},newstring[]{"6","d"},newstring[]{"9","a"},newstring[]{"6","e"}};varresult=list.Select(x=>x.ToList()).ToList();result.ForEach(x=>{if(result.Where(y=>y!=x).Any(y=>y.Any(z=>x.Contains(z)))){result.Where(y=>y!=x).Where(y=>y.Any(z=>x.Contains(z))).First().AddRange(x);x.Clear();}});result=result.Where(x=>x.Count>0).ToList();foreach(variteminresult){Console.WriteLine(string.Join(",",item.Distinct()));}}}}
代码简化了下。
解决方案九:
该回复于2012-04-18 08:55:14被版主删除
解决方案十:
并查集问题,可以O(n)的
解决方案十一:
List<string[]>a=newList<string[]>();a.Add(newstring[]{"1","11"});a.Add(newstring[]{"2","11"});a.Add(newstring[]{"2","12"});a.Add(newstring[]{"3","12"});a.Add(newstring[]{"4","13"});a.Add(newstring[]{"5","13"});a.Add(newstring[]{"5","14"});a.Add(newstring[]{"6","14"});a.Add(newstring[]{"7","16"});a.Add(newstring[]{"8","16"});a.Add(newstring[]{"9","15"});a.Add(newstring[]{"10","15"});a.Add(newstring[]{"11","1"});a.Add(newstring[]{"11","2"});a.Add(newstring[]{"12","2"});a.Add(newstring[]{"12","3"});a.Add(newstring[]{"13","4"});a.Add(newstring[]{"13","5"});a.Add(newstring[]{"14","4"});a.Add(newstring[]{"14","6"});a.Add(newstring[]{"15","9"});a.Add(newstring[]{"15","10"});a.Add(newstring[]{"16","7"});a.Add(newstring[]{"16","8"});
引用3楼的回复:
谢谢你们
你的这个结果怎么不对呢??融合后的结果是111212413154716810159而实际的结果是12311124561314781691015
解决方案十二:
呵呵,我的代码没问题,你可以试试看。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){varlist=newList<string[]>();list.Add(newstring[]{"1","11"});list.Add(newstring[]{"2","11"});list.Add(newstring[]{"2","12"});list.Add(newstring[]{"3","12"});list.Add(newstring[]{"4","13"});list.Add(newstring[]{"5","13"});list.Add(newstring[]{"5","14"});list.Add(newstring[]{"6","14"});list.Add(newstring[]{"7","16"});list.Add(newstring[]{"8","16"});list.Add(newstring[]{"9","15"});list.Add(newstring[]{"10","15"});list.Add(newstring[]{"11","1"});list.Add(newstring[]{"11","2"});list.Add(newstring[]{"12","2"});list.Add(newstring[]{"12","3"});list.Add(newstring[]{"13","4"});list.Add(newstring[]{"13","5"});list.Add(newstring[]{"14","4"});list.Add(newstring[]{"14","6"});list.Add(newstring[]{"15","9"});list.Add(newstring[]{"15","10"});list.Add(newstring[]{"16","7"});list.Add(newstring[]{"16","8"});varresult=list.Select(x=>x.ToList()).ToList();result.ForEach(x=>{if(result.Where(y=>y!=x).Any(y=>y.Any(z=>x.Contains(z)))){result.Where(y=>y!=x).Where(y=>y.Any(z=>x.Contains(z))).First().AddRange(x);x.Clear();}});result=result.Where(x=>x.Count>0).ToList();foreach(variteminresult){Console.WriteLine(string.Join(",",item.Distinct()));}}}}
12,3,2,11,114,6,4,13,515,10,916,8,7Pressanykeytocontinue...
解决方案十三:
谢谢啊,我试试啊!~引用11楼的回复:
呵呵,我的代码没问题,你可以试试看。C#codeusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1{classProgram{stati……