Ejemplo n.º 1
0
        private static string Shortest(SuffixTree tree)
        {
            var shortest = new Match(tree.Root, Enumerable.Empty <SuffixTree.Node>());

            var queue = new Queue <Match>();

            queue.Enqueue(shortest);
            while (queue.Any())
            {
                var current = queue.Dequeue();
                if (shortest.Node == tree.Root ||
                    (tree.ToNodeText(current.Node).EndsWith("$") && current.Node.Length > 1 && current.Distance() <= shortest.Distance()))
                {
                    shortest = current;
                }
                var l = new List <SuffixTree.Node>(current.Lineage);
                l.Add(current.Node);

                foreach (var child in current.Node.Children)
                {
                    queue.Enqueue(new Match(child, l));
                }
            }
            var s = string.Format("{0}{1}", string.Join("", shortest.Lineage.Select(tree.ToText)), tree.ToText(shortest.Node)).Replace("$", "");

            return(s);
        }
Ejemplo n.º 2
0
 public static SuffixTree ToSuffixTree(string text)
 {
     var result = new SuffixTree(text);
     //Console.WriteLine(string.Join(", ", result.ToDebugText()));
     for (var i = 0; i < text.Length; i++)
     {
         result.Merge(i, text.Length - i);
         //Console.WriteLine(string.Join(", ", result.ToDebugText()));
     }
     return result;
 }