Advertisement
Guest User

Untitled

a guest
Sep 6th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         var dict = new Dictionary<string, HashSet<string>>();
  10.         string[] delimiters = { " -> " };
  11.  
  12.         string input;
  13.         while (!(input = Console.ReadLine()).Equals("Blaze it!"))
  14.         {
  15.             var data = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
  16.             if (!data[0].Equals(data[1]))
  17.             {
  18.                 if (!dict.ContainsKey(data[0]))
  19.                 {
  20.                     dict.Add(data[0], new HashSet<string>());
  21.                 }
  22.                 dict[data[0]].Add(data[1]);
  23.             }
  24.         }
  25.  
  26.         var ordered = dict
  27.             .Select(kvp => new { kvp.Key, c = Count(dict, kvp.Key) })
  28.             .OrderByDescending(o => o.c)
  29.             .Select(o=>$"{o.Key} : {o.c}");
  30.  
  31.         Console.WriteLine(string.Join(Environment.NewLine, ordered));
  32.     }
  33.  
  34.     private static int Count(Dictionary<string, HashSet<string>> dict, string key)
  35.     {
  36.         int count = dict[key].Count;
  37.         foreach (var mate in dict[key])
  38.         {
  39.             if (dict.TryGetValue(mate, out var val) && val.Contains(key))
  40.             {
  41.                 count--;
  42.             }
  43.         }
  44.         return count;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement