这是一种情况。比如我有一个这样的结构(代码简化):
类多米诺 {男星多米诺(左,右) 串LeftSide; 串RightSide; }和我有数据,有点像这样的:
多米诺(2,3),多米诺(1,2),多米诺(4,5),多米诺(3,4)我知道不会有任何间隙多米诺骨牌,也不重复。 我需要订购此集合,所以每RightSide将连接到适当LeftSide。像这样:
多米诺(1,2),多米诺(2,3),多米诺(3,4),多米诺(4,5)值 - 而不是数字。只是需要一个线索。
现在我在2个步骤完成此任务。小学 - 我在找切入点。已LeftSide没有在任何其他多米诺RightSide呈现的多米诺。从那以后,我与0的索引项打开它。其次 - 我正在寻找具有循环LeftSide同我进入多米诺的RightSide等下一个多米诺
我在C#中这样做,但这种其实并不重要。
现在的问题是 - 我不认为这是最好的算法。任何想法将是巨大的。 THX。
EDITED!
是我不好谈数字。
让我们改变多米诺为trevel卡
所以它会是这样的:
旅行通票(都柏林,纽约),旅行卡(莫斯科,都柏林),旅行卡(纽约,哈瓦那 )解决方案
除非你有你的卡解决方案数额巨大将工作。否则,你可以考虑2字典,使搜索常量和保持 O(N)的复杂性:
命名空间ConsoleApplication {公共类多米诺 {公共多米诺(INT左,诠释右) { LeftSide =左; RightSide =权利; } 公众诠释LeftSide; 公众诠释RightSide; } 类节目 {静态无效的主要(字串[] args) { VAR输入=新的List<多米诺> () {新多米诺(2,3),新多米诺(1,2),新多米诺(4,5),新多米诺( 3,4)}; 变种dicLeft =新词典< INT,多米诺>(); 变种dicRigth =新词典< INT,多米诺>(); 的foreach(输入VAR项) { dicLeft.Add(item.LeftSide,项目); dicRigth.Add(item.RightSide,项目); } 多米诺第一= NULL; 的foreach(输入VAR项) {如果 {第一项=(dicRigth.ContainsKey(item.LeftSide)!); 中断; } } Console.WriteLine(的String.Format({0} - {1},first.LeftSide,first.RightSide)); 的for(int i = 0; I< input.Count - 1;我++) {第一= dicLeft [first.RightSide] Console.WriteLine(的String.Format({0} - {1},first.LeftSide,first.RightSide)); } 到Console.ReadLine(); } } }
Here is a situation. For example I have a structure like this (code is simplified):
class Dominoe { ctor Dominoe(left, right) string LeftSide; string RightSide; }And I have data, somewhat like this:
Dominoe("2", "3"), Dominoe("1", "2"), Dominoe("4", "5"), Dominoe("3", "4")I know that there won't be any gaps in dominoes, nor repeats. I need to order this collection, so every RightSide would be connected to appropriate LeftSide. Like so:
Dominoe("1", "2"), Dominoe("2", "3"), Dominoe("3", "4"), Dominoe("4", "5")Values - not numbers. Just need a clue.
Right now I've done this task in 2 steps. Primary - i'm looking for the entry point. The dominoe which have LeftSide not presented in any other Dominoe RightSide. After that i switch it with 0 index item. Secondly - i'm looking for next dominoe which have LeftSide same as RightSide of my entry dominoe and so on in cycle.
I'm doing this in C#, but this really doesn't matter.
The problem is - i don't think it's best algorithm. Any ideas will be great. Thx.
EDITED !
Was my bad to talk about numbers.
Let's change Dominoe for trevel cards.
So it'll be like:
TravelCard ("Dublin", "New York"), TravelCard ("Moscow", "Dublin"), TravelCard ("New York", "Habana")解决方案
Unless you have huge amount of your cards your solution will work. Otherwise you can consider 2 dictionaries to make searches constants and keep O(N) complexity:
namespace ConsoleApplication { public class Dominoe { public Dominoe(int left, int right) { LeftSide = left; RightSide = right; } public int LeftSide; public int RightSide; } class Program { static void Main(string[] args) { var input = new List<Dominoe>() { new Dominoe(2, 3), new Dominoe(1, 2), new Dominoe(4, 5), new Dominoe(3, 4) }; var dicLeft = new Dictionary<int, Dominoe>(); var dicRigth = new Dictionary<int, Dominoe>(); foreach (var item in input) { dicLeft.Add(item.LeftSide, item); dicRigth.Add(item.RightSide, item); } Dominoe first = null; foreach(var item in input) { if (!dicRigth.ContainsKey(item.LeftSide)) { first = item; break; } } Console.WriteLine(string.Format("{0} - {1}", first.LeftSide, first.RightSide)); for(int i = 0; i < input.Count - 1; i++) { first = dicLeft[first.RightSide]; Console.WriteLine(string.Format("{0} - {1}", first.LeftSide, first.RightSide)); } Console.ReadLine(); } } }
更多推荐
需要复杂的对象进行排序,像多米诺
发布评论