Advertisement
TimmyChannel

3

Apr 16th, 2024
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class DirectoryNode
  6. {
  7.     public string Name { get; set; }
  8.     public Dictionary<string, DirectoryNode> Children { get; set; }
  9.  
  10.     public DirectoryNode(string name)
  11.     {
  12.         Name = name;
  13.         Children = new Dictionary<string, DirectoryNode>();
  14.     }
  15.  
  16.     public void AddPath(string[] pathParts, int index = 0)
  17.     {
  18.         if (index < pathParts.Length)
  19.         {
  20.             var part = pathParts[index];
  21.             if (!Children.ContainsKey(part))
  22.             {
  23.                 Children[part] = new DirectoryNode(part);
  24.             }
  25.             Children[part].AddPath(pathParts, index + 1);
  26.         }
  27.     }
  28.  
  29.     // Вывод дерева с сортировкой при выводе
  30.     public void Print(string indent = "")
  31.     {
  32.         Console.WriteLine(indent + Name);
  33.         foreach (var child in Children.Keys.OrderBy(key => key))
  34.         {
  35.             Children[child].Print(indent + "  ");
  36.         }
  37.     }
  38. }
  39.  
  40. class Program
  41. {
  42.     static void Main()
  43.     {
  44.         int n = int.Parse(Console.ReadLine());
  45.         var root = new DirectoryNode("root");
  46.  
  47.         for (int i = 0; i < n; i++)
  48.         {
  49.             string path = Console.ReadLine();
  50.             string[] parts = path.Split('/');
  51.             if (parts[0] == "root")  // Убедитесь, что путь начинается с "root"
  52.             {
  53.                 root.AddPath(parts, 1);
  54.             }
  55.             else
  56.             {
  57.                 root.AddPath(parts);  // Если путь не начинается с "root", передавайте его полностью
  58.             }
  59.         }
  60.  
  61.         root.Print();
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement