62,046
社区成员
发帖
与我相关
我的任务
分享
List<string[]> list = new List<string[]>()
{
new string[]{"专业","美国","XX"} ,
new string[]{"专业","美国"} ,
new string[]{"专业","XX"} ,
new string[]{"美国","XX"} ,
new string[]{"专业"} ,
new string[]{"美国"} ,
new string[]{"XX"} ,
};
static string[] str = { "专业", "美国", "XX", "JJ" };
static void GetDic(Dictionary<string, int> dd, List<string> lst)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> kk in dd)
{
for (int i = kk.Value + 1; i < str.Length; i++)
{
string key = kk.Key + "==" + str[i];
//Console.WriteLine(kk.Key + "==" + str[i]);
lst.Add(key);
dic.Add(key, i);
}
}
//递归
if (dic.Count > 0) GetDic(dic, lst);
}
static void Main(string[] args)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
//将数组元素添加到dic
for (int i = 0; i < str.Length; i++)
{
dic.Add(str[i], i);
}
List<string> lst = new List<string>();
GetDic(dic, lst);
lst.AddRange(str);
lst = lst.OrderByDescending(x => x.Count(y => y == '=')).ToList();
lst.ForEach(x =>
{
Console.WriteLine(x);
});
}
class Class1
{
static string[] str = { "专业", "美国", "XX","JJ" };
static List<string[]> lst = new List<string[]> { str};
static void Main()
{
Dictionary<string, int> dic = new Dictionary<string, int>();
//将数组元素添加到dic
for (int i = 0; i < str.Length; i++)
{
dic.Add(str[i], i);
}
GetDic(dic);
lst.AddRange(str.Select(x => new string[] { x }));
foreach (var arr in lst)
{
for (var i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + "===");
}
Console.WriteLine();
}
Console.ReadLine();
}
static void GetDic(Dictionary<string, int> dd)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> kk in dd)
{
for (int i = kk.Value + 1; i < str.Length; i++)
{
// Console.WriteLine(kk.Key +"=="+ str[i]);
lst.Add(new string[] { kk.Key, str[i] });
// dic.Add(kk.Key +"RRR"+ str[i], i);
}
}
//递归
if (dic.Count > 0) GetDic(dic);
}
}
我这样是可以弄出上面的效果,但是我在加个JJ,他就不对了,第二行应该是专业====美国===xx,第三行应该是专业===xx==jjlst.AddRange(str.Select(x=>new string[]{x}))
最终再执行下这个就好了啊
string str = "专业,美国,XX";
string[] arr = str.Split(',');
List<string[]> list = new List<string[]>();
list.Add(arr);
for (int i = 0; i < arr.Length; i++)
{
list.Add(new string[] { arr[i] });
for (int k = 0; k < arr.Length; k++)
{
if (k > i)
{
list.Add(new string[] { arr[i], arr[k] });
}
}
}
list.OrderByDescending(p=>p.Length).ToList().ForEach(p => Console.WriteLine(string.Join(",", p)));
static void Main(string[] args)
{
string str = "专业,美国,XX";
string[] sArr = str.Split(',');
List<string[]> outList = addList(sArr.Select(p => new string[] { p }).ToList(), sArr, 1).OrderByDescending(p => p.Length).ToList();
outList.ForEach(p => Console.WriteLine(string.Join(",", p)));
}
static List<string[]> addList(List<string[]> list, string[] arr, int len)
{
for (int i = 0; i < arr.Length; i++)
{
List<string[]> fitList = list.Where(p => p.Length == len && !p.Contains(arr[i])).ToList();
foreach (string[] s in fitList)
{
string[] ss = s.Concat(new string[] { arr[i] }).ToArray();
if (!list.Any(p => p.Length == len + 1 && p.Intersect(ss).Count() == p.Length))
{
list.Add(ss);
}
}
}
if (len < arr.Length - 1) addList(list, arr, len + 1);
return list;
}
如果string[]仅是定长为3的话,那更简单了:
Console.WriteLine(kk.Key + "==" + str[i]);
dic.Add(kk.Key + "==" + str[i], i);
class Class1
{
static string[] str = { "专业", "美国", "XX" };
static void Main()
{
Dictionary<string, int> dic = new Dictionary<string, int>();
//将数组元素添加到dic
for (int i = 0; i < str.Length; i++)
{
dic.Add(str[i], i);
}
List<string[]> lst=new List<string[]>();
GetDic(dic);
Console.ReadLine();
}
static void GetDic(Dictionary<string, int> dd)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> kk in dd)
{
for (int i = kk.Value + 1; i < str.Length; i++)
{
Console.WriteLine(kk.Key +"=="+ str[i]);
dic.Add(kk.Key + str[i], i);
}
}
//递归
if (dic.Count > 0) GetDic(dic);
}
}