Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 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 _04.Group_Continents__Countries_and_Cities
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var continentData = new SortedDictionary<string, SortedDictionary<string, SortedSet<string>>>();
  14.             var n = int.Parse(Console.ReadLine());
  15.  
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 var tokens = Console.ReadLine().Split();
  19.                 var continent = tokens[0];
  20.                 var country = tokens[1];
  21.                 var city = tokens[2];
  22.  
  23.                 if (!continentData.ContainsKey(continent))
  24.                 {
  25.                     continentData.Add(continent, new SortedDictionary<string, SortedSet<string>>());
  26.                     continentData[continent] = new SortedDictionary<string, SortedSet<string>>
  27.                     {
  28.                         {country, new SortedSet<string>()}
  29.                     };
  30.                 }
  31.                 else
  32.                 {
  33.                     if (!continentData[continent].ContainsKey(country))
  34.                     {
  35.                         continentData[continent].Add(country, new SortedSet<string>());
  36.                     }
  37.                 }
  38.  
  39.                 continentData[continent][country].Add(city);
  40.             }
  41.  
  42.             foreach (var continentContries in continentData)
  43.             {
  44.                 var continentName = continentContries.Key;
  45.                 var countries = continentContries.Value;
  46.  
  47.                 Console.WriteLine($"{continentName}:");
  48.  
  49.                 foreach (var countryCities in countries)
  50.                 {
  51.                     var countryName = countryCities.Key;
  52.                     var cities = countryCities.Value;
  53.                     Console.WriteLine($"  {countryName} -> {string.Join(", ", cities)}");
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement