您的位置:首页 > 编程语言 > C#

c#组合算法整理

2009-02-24 11:38 232 查看
原帖http://topic.csdn.net/u/20090220/23/e2d130d9-d7d4-4520-bec7-e78ae6ca9aff.html

感谢litaoye和min_jie

static List<string> GetCombinationF3(string[] data, int count)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
List<string> output = new List<string>();
for (int i = 0; i < data.Length; i++)
{
dic.Add(data[i], i);
}
SelectN(dic, data, count, 1, ref output);
return output;
}

static void SelectN(Dictionary<string, int> dd, string[] data, int count, int times, ref List<string> output)
{
Dictionary<string, int> dic = new Dictionary<string, int>();

foreach (KeyValuePair<string, int> kv in dd)
{
for (int i = kv.Value + 1; i < data.Length; i++)
{
if (times < count - 1)
{
dic.Add(kv.Key + "/t" + data[i], i);
}
else
{
output.Add(kv.Key + "/t" + data[i]);//不考虑输出,将此句注释掉
}
}
}
times++;
if (dic.Count > 0) SelectN(dic, data, count, times,ref output);
}

static List<string> GetCombinationF1(string[] set,int m)
{
int n = set.Length;
int min = (0x01 << m) - 1;//00111111
int max = min << (n - m);//11111100
int j;
int k;
List<string> output = new List<string>();
string s;

for (int i = min; i <= max; i++)
{
j = 0;
k = i;
while (k > 0)
{
j += (int)(k & 0x01);
k >>= 1;
if (j > m)
{
break;
}
}
if (j == m)
{
s = "";
k = 0x01;
for (int l = n - 1; l >= 0; l--)
{
if ((k & i) == k)
{
s+=set[l] + "/t";
}
k <<= 1;
}
output.Add(s);
}

}
return output;
}

static List<string> GetCombinationF2(string[] strArray, int selectCount)
{
int totalCount = strArray.Length;
int[] currentSelect = new int[selectCount];
int last = selectCount - 1;
List<string> output = new List<string>();
string s;

//付初始值
for (int i = 0; i < selectCount; i++)
currentSelect[i] = i;

while (true)
{
s = "";
//输出部分,生成的时候从0计数,所以输出的时候+1
for (int i = 0; i < selectCount; i++)
{
s += strArray[currentSelect[i]]+"/t";
}
output.Add(s);

//如果不进位
if (currentSelect[last] < totalCount - 1)
currentSelect[last]++;
else
{
//进位部分
int position = last;

while (position > 0 && currentSelect[position - 1] == currentSelect[position] - 1)
position--;

if (position == 0)
break ;

currentSelect[position - 1]++;

for (int i = position; i < selectCount; i++)
currentSelect[i] = currentSelect[i - 1] + 1;
}
}
return output;
}

static List<string> GetCombinationF4(string[] data, int count)
{
List<string> output = new List<string>();
int len = data.Length;
string start = "1".PadRight(count, '1').PadRight(len, '0');
string s;
while (start != string.Empty)
{
s = "";
for (int i = 0; i < len; i++)
if (start[i] == '1') s+=data[i] + "/t";
output.Add(s);
start = GetNext(start);
}
return output;

}

static string GetNext(string str)
{
string next = string.Empty;
int pos = str.IndexOf("10");
if (pos < 0) return next;
else if (pos == 0) return "01" + str.Substring(2);
else
{
int len = str.Length;
next = str.Substring(0, pos).Replace("0", "").PadRight(pos, '0') + "01";
if (pos < len - 2) next += str.Substring(pos + 2);
}
return next;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: