Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace Test
  7. {
  8.  
  9.     class Program
  10.     {
  11.  
  12.         private static void Main(string[] args)
  13.         {
  14.             int count = int.Parse(Console.ReadLine());
  15.             var dicionary = new Dictionary<string, int>();
  16.             List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();
  17.  
  18.             for (int i = 0; i < count; i++)
  19.             {
  20.                 string[] str = Console.ReadLine().Split(' ');
  21.                 int countStr = int.Parse(str[1]);
  22.  
  23.                 if (dicionary.ContainsKey(str[0]))
  24.                 {
  25.                     dicionary[str[0]] += countStr;
  26.                 }
  27.                 else
  28.                 {
  29.                     dicionary.Add(str[0], countStr);
  30.                 }
  31.             }
  32.  
  33.             count = int.Parse(Console.ReadLine());
  34.             var liststr = new List<string>();
  35.  
  36.             for (int i = 0; i < count; i++)
  37.             {
  38.                 liststr.Add(Console.ReadLine());
  39.             }
  40.  
  41.             var ls = new List<int>();
  42.  
  43.             for (int i = 0; i < liststr.Count; i++)
  44.             {
  45.                 var str = liststr[i];
  46.                 list = new List<KeyValuePair<string, int>>();
  47.  
  48.                 foreach (var el in dicionary)
  49.                 {
  50.                     if (el.Key.StartsWith(str))
  51.                     {
  52.                         list.Add(el);
  53.                     }
  54.  
  55.                 }
  56.  
  57.                 list.Sort((value1, value2) =>
  58.                 {
  59.                     if (value1.Value > value2.Value)
  60.                         return 1;
  61.  
  62.                     if (value1.Value < value2.Value)
  63.                         return -1;
  64.  
  65.                     if (value1.Value == value2.Value)
  66.                     {
  67.                         int index = 0;
  68.  
  69.                         var str1 = value1.Key.ToLower();
  70.                         var str2 = value2.Key.ToLower();
  71.  
  72.                         while (index < str1.Length && index < str2.Length
  73.                         && str1[index] == str2[index])
  74.                         {
  75.                             index++;
  76.                         }
  77.  
  78.                         if (index == value1.Key.Length && index != value2.Key.Length)
  79.                         {
  80.                             return 1;
  81.                         }
  82.  
  83.                         if (index == value2.Key.Length && index != value1.Key.Length)
  84.                         {
  85.                             return -1;
  86.                         }
  87.  
  88.                         if (index == value1.Key.Length)
  89.                         {
  90.                             index--;
  91.                         }
  92.  
  93.                         if (str1[index] > str2[index])
  94.                             return -1;
  95.  
  96.                         else if (str1[index] < str2[index])
  97.                             return 1;
  98.  
  99.                     }
  100.  
  101.                     return 0;
  102.  
  103.                 });
  104.  
  105.                 list.Reverse();
  106.  
  107.                 int ind = 0;
  108.  
  109.                 if (i != 0)
  110.                 {
  111.                     if (list.Count == 0 || ls[ls.Count - 1] == 0)
  112.                     {
  113.                         if (ls.Count - 1 != 0)
  114.                             Console.WriteLine();
  115.                     }
  116.                     else
  117.                     {
  118.                         Console.WriteLine();
  119.                         Console.WriteLine();
  120.                     }
  121.                 }
  122.                 else
  123.                 {
  124.                     if (list.Count == 0)
  125.                     {
  126.                         Console.WriteLine();
  127.                     }
  128.                 }
  129.  
  130.                 while (ind < 10 && ind < list.Count)
  131.                 {
  132.                     if (ind < 9 && ind < list.Count - 1)
  133.                         Console.WriteLine(list[ind++].Key);
  134.                     else
  135.                         Console.Write(list[ind++].Key);
  136.                 }
  137.  
  138.                 ls.Add(list.Count);
  139.             }
  140.  
  141.  
  142.         }
  143.     }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement