Infiniti_Inter

OLD

Apr 7th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7.  
  8.  
  9. public class BinaryTree
  10. {
  11.  
  12.  
  13.     Node tree;
  14.     public BinaryTree() { tree = null; }
  15.  
  16.  
  17.     public void Solve(string s)
  18.     {
  19.         List<Node> tree = new List<Node>();
  20.         SortedDictionary<char, int> cnt = new SortedDictionary<char, int>();
  21.         for (int i = 0; i < s.Length; ++i)
  22.         {
  23.             if (cnt.ContainsKey(s[i]))
  24.                 cnt[s[i]]++;
  25.             else
  26.                 cnt.Add(s[i], 1);
  27.         }
  28.         foreach (var v in cnt)
  29.         {
  30.             Node cur = new Node(v.Value, v.Key);
  31.             tree.Add(cur);
  32.         }
  33.  
  34.  
  35.         while (tree.Count != 1)
  36.         {
  37.             tree.Sort((a, b) => a.inf.CompareTo(b.inf));
  38.  
  39.             Node l = tree[0];
  40.             tree.RemoveAt(0);
  41.  
  42.             Node r = tree[0];
  43.             tree.RemoveAt(0);
  44.  
  45.             Node parent = new Node(ref l, ref r);
  46.             tree.Add(parent);
  47.         }
  48.         Node root = tree[0];
  49.         Node.Print(ref root);
  50.  
  51.         Node.Build(ref root);
  52.         List<int> path = new List<int>();
  53.         Node.dfs(root, path);
  54.  
  55.  
  56.  
  57.     }
  58.  
  59.  
  60.  
  61.     public class Node
  62.     {
  63.  
  64.         public int inf;
  65.         public Node left;
  66.         public Node right;
  67.         char c;
  68.         int code = -1;
  69.  
  70.         public static void dfs(Node root, List<int> path)
  71.         {
  72.             if (root.c != '~')
  73.             {
  74.                 Console.Write($"{root.c} = ");
  75.                 foreach (var v in path)
  76.                     Console.Write(v);
  77.                 Console.WriteLine(root.code);
  78.  
  79.             }
  80.             if (root.code != -1)
  81.                 path.Add(root.code);
  82.             if (root.left != null)
  83.                 dfs(root.left, path);
  84.             if (root.right != null)
  85.                 dfs(root.right, path);
  86.             if (path.Count > 0)
  87.                 path.RemoveAt(path.Count - 1);
  88.         }
  89.  
  90.         static public void Print(ref Node root, int depth = 0)
  91.         {
  92.             if (root == null)
  93.                 return;
  94.  
  95.             if (root.c != '~')
  96.             {
  97.                 for (int i = 0; i < depth; i++)
  98.                     Console.Write(".");
  99.                 Console.WriteLine(root.c);
  100.  
  101.             }
  102.             else depth++;
  103.             Print(ref root.left, depth);
  104.             Print(ref root.right, depth);
  105.         }
  106.  
  107.  
  108.         public Node(int nodeInf, char nodeChar)
  109.         {
  110.             inf = nodeInf;
  111.             c = nodeChar;
  112.             left = null;
  113.             right = null;
  114.         }
  115.  
  116.         public Node() { left = right = null; }
  117.         public Node(ref Node L, ref Node R)
  118.         {
  119.  
  120.             left = L;
  121.             right = R;
  122.             inf = L.inf + R.inf;
  123.             c = '~';
  124.         }
  125.  
  126.         public static void Build(ref Node root)
  127.         {
  128.  
  129.  
  130.             if (root.left != null)
  131.             {
  132.                 root.left.code = 0;
  133.                 Build(ref root.left);
  134.  
  135.             }
  136.             if (root.right != null)
  137.             {
  138.                 root.right.code = 1;
  139.                 Build(ref root.right);
  140.  
  141.             }
  142.         }
  143.  
  144.  
  145.  
  146.     }
  147.  
  148. }
  149.  
  150.  
  151.  
  152. class Program
  153. {
  154.     static void Main(string[] args)
  155.     {
  156.  
  157.  
  158.         string s = $"Общее решение однородного уравнения выписано верно " +
  159.             $"Разбейте, пожалуйста, задания по номерам, сейчас наблюдаю некое несоответствие." +
  160.             $"Зачем во втором задании(решение однородного уравнения с некоторыми начальными условиями) построена такая система? Какие начальные условия были выбраны? Или это уже следующее задание(для следующего там не хвататет производных у С_i) ?" +
  161.             $"Была ли выполнена проверка(подcтановкой) для решения, которое было получено операторным методом? Штрих около h(t) в итоговом решении - производная ? Пожалуйста, проверьте.";
  162.            
  163.         BinaryTree tree = new BinaryTree();
  164.         tree.Solve(s);
  165.  
  166.  
  167.     }
  168.  
  169. }
Add Comment
Please, Sign In to add comment