using System.Collections.Generic;
public CustomNode Parent;
public List<CustomNode> Children;
public CustomNode(string title, CustomNode parent)
Children = new List<CustomNode>();
Parent.Children.Add(this);
public CustomNode Find(string path)
string[] pieces = path.Split(new char[] { '/' });
foreach (var child in Children)
if (child.Title == pieces[1])
return child.Find(path.Remove(0, Title.Length + 1));
public static void Main()
CustomNode root = new CustomNode("Root", null);
CustomNode userData = new CustomNode("UserData", root);
CustomNode ud_browser = new CustomNode("Browser", userData);
CustomNode ud_word = new CustomNode("Word", userData);
CustomNode priv = new CustomNode("Private", userData);
CustomNode priv_word = new CustomNode("Word", priv);
CustomNode windows = new CustomNode("Windows", root);
CustomNode programs = new CustomNode("Programs", root);
CustomNode notepad = new CustomNode("Notepad", programs);
CustomNode prog_word = new CustomNode("Word", programs);
CustomNode prog_browser = new CustomNode("Browser", programs);
CustomNode target = root.Find(Console.ReadLine());
Console.WriteLine(GetShortestUniqueQualifier(root, target));
public static string GetShortestUniqueQualifier(CustomNode root, CustomNode target)
if (root == null || target == null) return null;
if (target == root) return target.Title;
if (target.Parent == root){
if (root.Title == target.Title) return root.Title + "/" + target.Title;
else return target.Title;
Stack<Tuple<string, bool>> suqStack = new Stack<Tuple<string, bool>>();
suqStack.Push( new Tuple<string, bool>(target.Title, true) );
CustomNode curr = target.Parent.Parent;
foreach (CustomNode c in curr.Children){
foreach (CustomNode d in c.Children){
if (d.Title == target.Title){
suqStack.Push( new Tuple<string, bool>(target.Parent.Title, found) );
while (suqStack.Count > 0){
var tempTuple = suqStack.Pop();
suqString += tempTuple.Item1;
while (suqStack.Count > 0) suqString += "/" + suqStack.Pop().Item1;