using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
string[] str1 = { "A", "A", "C", "E", "F" };
string[] str2 = { "B", "C", "D", "F", "G" };
int[] re = { 1, 0, 0, 0, 0, };
int index = 2;
for (int i = 1; i < str1.Length; i++)
{
int st = 0;
for (int j = 0; j <= i-1; j++)
{
if((str1[i]==str1[j]) || (str2[i]==str1[j]) || (str1[i] == str2[j]) || (str2[i]==str2[j]))
{
re[i] = re[j];
st = 1;
break;
}
}
if (st == 0)
{
re[i] = index;
index++;
}
}
List<List<string>> res= new List<List<string>>();
for (int i = 0; i < index-1; i++)
{
res.Add(new List<string>());
}
for (int i = 0; i < str1.Length; i++)
{
if (!res[re[i]-1].Contains(str1[i]))
{
res[re[i] - 1].Add(str1[i]);
}
if (!res[re[i] - 1].Contains(str2[i]))
{
res[re[i] - 1].Add(str2[i]);
}
}
Console.Write("{");
for (int i = 0; i < res.Count; i++)
{
Console.Write("{");
for (int j = 0; j < res[i].Count; j++)
{
Console.Write(res[i][j]);
if (j==res[i].Count-1)
{
break;
}
Console.Write(",");
}
if (i==res.Count-1)
{
Console.Write("}");
}
else
{
Console.Write("},");
}
}
Console.Write('}');
Console.ReadKey();
}
}
}
1.先把所有左边出现的和右边出现的统一加入一个“池子”里,比如一个list,加入前先判断contains,不要重复加入
2.new一个list,从池子里取一个加入进来,取出的就从池子中删去,
3.去对应表里左边找对应项,看是否有对应的右边,有就也加入进来;再去右边找,把左边的也加入进来
4.把过程3进行递归,去找下一项的下一项,直到没有下一项,就结束
5.重复过程2,3,4,直到list清空
了解他是什么比给代码有意义,所以我不给代码,只告诉你这是什么
这是有向图,有向图可以有很多总实现。但比较常见的是邻接表,邻接矩阵。
邻接表
https://baike.baidu.com/item/%E9%82%BB%E6%8E%A5%E8%A1%A8/9796152?fr=aladdin
主体就是构建邻接表,构建邻接表后代码反而没啥了,也就是单纯遍历
trie 树
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!