svetliovd

Untitled

Jul 9th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Dict_Ref_Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var entries = new Dictionary<string, List<int>>();
  14.             int n;
  15.             while (true)
  16.             {
  17.                 string[] input = Console.ReadLine()
  18.                     .Split(new string[] { " -> " }, StringSplitOptions.None).ToArray();
  19.                 if (input[0] == "end") { break; }
  20.                 string[] values = input[1]
  21.                     .Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  22.                 string name = input[0];
  23.                 if (!entries.ContainsKey(name))
  24.                 {
  25.                     if (!int.TryParse(values[0], out n) && !entries.ContainsKey(values[0]))
  26.                     {
  27.                         continue;
  28.                     }
  29.                     entries.Add(name, new List<int>());
  30.                 }
  31.                 for (int i = 0; i < values.Length; i++)
  32.                 {
  33.                     if (int.TryParse(values[i], out n))
  34.                     {
  35.                         entries[name].Add(n);
  36.                     }
  37.                     else
  38.                     {
  39.                         if (entries.ContainsKey(values[i]))
  40.                         {
  41.                             entries[name] = new List<int>();
  42.                             for (int j = 0; j < entries[values[i]].Count; j++)
  43.                             {
  44.                                 entries[name].Add(entries[values[i]][j]);
  45.                             }
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.             foreach (KeyValuePair<string, List<int>> item in entries)
  51.             {
  52.                 Console.Write($"{item.Key} === ");
  53.                 Console.Write(string.Join(", ", item.Value));
  54.                 Console.WriteLine();
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment