Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class DirectoryNode
- {
- public string Name { get; set; }
- public Dictionary<string, DirectoryNode> Children { get; set; }
- public DirectoryNode(string name)
- {
- Name = name;
- Children = new Dictionary<string, DirectoryNode>();
- }
- public void AddPath(string[] pathParts, int index = 0)
- {
- if (index < pathParts.Length)
- {
- var part = pathParts[index];
- if (!Children.ContainsKey(part))
- {
- Children[part] = new DirectoryNode(part);
- }
- Children[part].AddPath(pathParts, index + 1);
- }
- }
- // Вывод дерева с сортировкой при выводе
- public void Print(string indent = "")
- {
- Console.WriteLine(indent + Name);
- foreach (var child in Children.Keys.OrderBy(key => key))
- {
- Children[child].Print(indent + " ");
- }
- }
- }
- class Program
- {
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- var root = new DirectoryNode("root");
- for (int i = 0; i < n; i++)
- {
- string path = Console.ReadLine();
- string[] parts = path.Split('/');
- if (parts[0] == "root") // Убедитесь, что путь начинается с "root"
- {
- root.AddPath(parts, 1);
- }
- else
- {
- root.AddPath(parts); // Если путь не начинается с "root", передавайте его полностью
- }
- }
- root.Print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement