首页 新闻 会员 周边

组合算法问题求助

0
悬赏园豆:50 [已解决问题] 解决于 2011-06-16 21:55

我想实现一个函数方法,具体如下:

1、函数func(string type,string str)。

2、参数type是由数字组成的字符串,如:"2" "23" "34" "234" ......

3、参数str形如:",001001,001002,001003,002001,002003,003004,003005,006001,006002,009001,009003,011001,011003"

str参数中有如下特点:

1、元素之间用逗号隔开

2、每个元素都由6位数字组成

3、前三位相同的元素可以当作是一组(元素组)

func函数特点:从str参数不同元素组中取出一个元素组成由type(2:2个,23:2个和3个)个元素组成的新元素

结果:如果type=2 func函数返回值是

+001001+002001| +001001+002003| +001002+002001| +001002+002003| +001003+002001| +001003+002003| +001001+003004| +001001+003005| +001002+003004| +001002+003005| +001003+003004| +001003+003005| +001001+006001| +001001+006002| +001002+006001| +001002+006002| +001003+006001| +001003+006002| +001001+009001| +001001+009003| +001002+009001| +001002+009003| +001003+009001| +001003+009003| +001001+011001| +001001+011003| +001002+011001| +001002+011003| +001003+011001| +001003+011003| +002001+003004| +002001+003005| +002003+003004| +002003+003005| +002001+006001| +002001+006002| +002003+006001| +002003+006002| +002001+009001| +002001+009003| +002003+009001| +002003+009003| +002001+011001| +002001+011003| +002003+011001| +002003+011003| +003004+006001| +003004+006002| +003005+006001| +003005+006002| +003004+009001| +003004+009003| +003005+009001| +003005+009003| +003004+011001| +003004+011003| +003005+011001| +003005+011003| +006001+009001| +006001+009003| +006002+009001| +006002+009003| +006001+011001| +006001+011003| +006002+011001| +006002+011003| +009001+011001| +009001+011003| +009003+011001| +009003+011003|

01之间穿梭的主页 01之间穿梭 | 初学一级 | 园豆:25
提问于:2011-06-15 17:11
< >
分享
最佳答案
0

View Code
1 勉强实现。。。
2 public string Translate(string type, string str)
3 {
4 StringBuilder sb = new StringBuilder();
5
6 IList<int> typeList = AnalyzeType(type);
7
8 foreach (int numType in typeList)
9 {
10 BuildResult(numType, AnalyzeStr(str), sb);
11 }
12 return sb.ToString();
13 }
14
15 private IList<int> AnalyzeType(string type)
16 {
17 IList<int> typeList = new List<int>();
18
19 Regex reg = new Regex(@"^\d{1}$");
20 Array arr = type.ToArray();
21
22 foreach (var character in arr)
23 {
24 if (reg.IsMatch(character.ToString()))
25 {
26 typeList.Add(int.Parse(character.ToString()));
27 }
28 }
29 return typeList;
30 }
31
32 private IList<string> AnalyzeStr(string str)
33 {
34 IList<string> list = new List<string>();
35 Regex reg = new Regex(@"^\d{6}$");
36
37 string[] content = str.Split(',');
38
39 foreach (string everyStr in content)
40 {
41 if (reg.IsMatch(everyStr))
42 {
43 list.Add(everyStr);
44 }
45 }
46
47 return list;
48 }
49
50 private void BuildResult(int type, IList<string> contentList, StringBuilder sb)
51 {
52 if (contentList.Count != 0)
53 {
54 string code = contentList.First();
55 contentList.Remove(code);
56
57 Dictionary<int, List<string>> dictionary = new Dictionary<int, List<string>>();
58 for (int j = 1; j < type; j++)
59 {
60 string[] list = new string[contentList.Count];
61 contentList.CopyTo(list, 0);
62 List<string> copyList = list.ToList<string>();
63 dictionary.Add(j, copyList);
64 }
65
66 while (dictionary.Values.Last().Count > 0)
67 {
68 bool addInsb = false;
69
70 StringBuilder newSb = new StringBuilder();
71
72 newSb.Append("+").Append(code);
73
74 foreach (List<string> list in dictionary.Values)
75 {
76 if (list != null && list.Count > 0)
77 {
78 string listFir = list.First();
79
80 //前3位相同的则为一组
81 if (!list.First().Substring(0, 3).Equals(code.Substring(0, 3)))
82 {
83 newSb.Append("+").Append(list.First());
84 addInsb = true;
85 }
86
87 foreach (List<string> listA in dictionary.Values)
88 {
89 listA.Remove(listFir);
90 }
91 }
92
93 if (!addInsb)
94 break;
95 }
96
97 newSb.Append("|");
98
99 if (addInsb)
100 {
101 sb.Append(newSb);
102 }
103
104 //因为移出了每次的第一个,必须使得循环从头开始
105 BuildResult(type, contentList, sb);
106 }
107 }
108 }

收获园豆:50
dwwwing | 小虾三级 |园豆:661 | 2011-06-16 14:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册