算法

zhanglong_longlong 2015-06-03 05:04:44
string str = "专业,美国,XX";已知这个变量,想得到以下顺序得数据



List<string[]> list = new List<string[]>()
{
new string[]{"专业","美国","XX"} ,
new string[]{"专业","美国"} ,
new string[]{"专业","XX"} ,
new string[]{"美国","XX"} ,
new string[]{"专业"} ,
new string[]{"美国"} ,
new string[]{"XX"} ,
};
...全文
295 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
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);
            });
}
zhanglong_longlong 2015-06-04
  • 打赏
  • 举报
回复
引用 7 楼 starfd 的回复:
lst.AddRange(str.Select(x=>new string[]{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==jj
zhanglong_longlong 2015-06-04
  • 打赏
  • 举报
回复
引用 7 楼 starfd 的回复:
lst.AddRange(str.Select(x=>new string[]{x}))
最终再执行下这个就好了啊
这个我知道的,但是,我希望他最后的专业 美国 XX 是单独的,换行这样,最终的效果需要这样存 List<string[]> list = new List<string[]>() { new string[]{"专业","美国","XX"} , new string[]{"专业","美国"} , new string[]{"专业","XX"} , new string[]{"美国","XX"} , new string[]{"专业"} , new string[]{"美国"} , new string[]{"XX"} , };
  • 打赏
  • 举报
回复
lst.AddRange(str.Select(x=>new string[]{x}))
最终再执行下这个就好了啊
  • 打赏
  • 举报
回复
最后的一个是你的一个全组合啊,你那个是一个个的效果,这个递归结束后,将数组每个值添加到你的list里面就可以了啊……
zhanglong_longlong 2015-06-04
  • 打赏
  • 举报
回复
引用 4 楼 starfd 的回复:
Console.WriteLine(kk.Key + "==" + str[i]);
dic.Add(kk.Key + "==" + str[i], i);
这样不是我要的效果,我希望他最后的专业 美国 XX 是单独的,换行这样
ajianchina 2015-06-04
  • 打赏
  • 举报
回复
如果你的情况string[]是定长为3的话,那更简单了:

            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)));
ajianchina 2015-06-04
  • 打赏
  • 举报
回复
虽然结贴了,我也来发一下我的代码,是我自己写的。


        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);
zhanglong_longlong 2015-06-03
  • 打赏
  • 举报
回复
zhanglong_longlong 2015-06-03
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
http://www.jb51.net/article/38205.htm
这样他最后的专业和美国是连在一起的

 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);
        }
    }

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧