Advertisement
grubcho

Key - key, Value - value

Jul 14th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 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. //Write a program, which searches for a key and value inside of several key-value pairs.
  7. //Input
  8. //•   On the first line, you will receive a key.
  9. //•   On the second line, you will receive a value.
  10. //•   On the third line, you will receive N.
  11. //•   On the next N lines, you will receive strings in the following format:
  12. //“key => {value 1};{value 2};…{value X}”
  13. //After you receive N key -> values pairs, your task is to go through them and print only the keys, which contain the key and the //values, which contain the value. Print them in the following format:
  14.  
  15. namespace Key___key_Value___Value
  16. {
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
  22.             string searchKey = Console.ReadLine();
  23.             string searchValue = Console.ReadLine();
  24.             int cnt = int.Parse(Console.ReadLine());
  25.             for (int i = 0; i < cnt; i++)
  26.             {
  27.                 string[] input = Console.ReadLine().Split(new string[] { " => " }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  28.                 string key = input[0];
  29.                 string values = input[1];
  30.                 string[] value = values.Split(';').ToArray();
  31.                 if (key.Contains(searchKey))
  32.                 {
  33.                     if (!dict.ContainsKey(key))
  34.                     {
  35.                         dict.Add(key, new List<string>());
  36.                     }
  37.                     for (int j = 0; j < value.Length; j++)
  38.                     {
  39.                         if (searchValue.Contains(value[j]) || value[j].Contains(searchValue))
  40.                         {
  41.                             dict[key].Add(value[j]);
  42.                         }                        
  43.                     }
  44.                 }
  45.                
  46.             }
  47.             foreach (var item in dict)
  48.             {
  49.                 Console.WriteLine("{0}:", item.Key);
  50.                 foreach (var element in item.Value)
  51.                 {
  52.                     Console.WriteLine("-{0}", element);
  53.                 }
  54.             }
  55.  
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement