Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //Write a program, which searches for a key and value inside of several key-value pairs.
- //Input
- //• On the first line, you will receive a key.
- //• On the second line, you will receive a value.
- //• On the third line, you will receive N.
- //• On the next N lines, you will receive strings in the following format:
- //“key => {value 1};{value 2};…{value X}”
- //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:
- namespace Key___key_Value___Value
- {
- class Program
- {
- static void Main(string[] args)
- {
- Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
- string searchKey = Console.ReadLine();
- string searchValue = Console.ReadLine();
- int cnt = int.Parse(Console.ReadLine());
- for (int i = 0; i < cnt; i++)
- {
- string[] input = Console.ReadLine().Split(new string[] { " => " }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- string key = input[0];
- string values = input[1];
- string[] value = values.Split(';').ToArray();
- if (key.Contains(searchKey))
- {
- if (!dict.ContainsKey(key))
- {
- dict.Add(key, new List<string>());
- }
- for (int j = 0; j < value.Length; j++)
- {
- if (searchValue.Contains(value[j]) || value[j].Contains(searchValue))
- {
- dict[key].Add(value[j]);
- }
- }
- }
- }
- foreach (var item in dict)
- {
- Console.WriteLine("{0}:", item.Key);
- foreach (var element in item.Value)
- {
- Console.WriteLine("-{0}", element);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement